cd "D:\JFQA submission"

****************************************************************************************************************************************************
/* Keep only required variables */
****************************************************************************************************************************************************

use "raw\LoansInMCA(2).dta", replace

keep creditorName creditorCode firmCode firmName firmCIN loanAmount sanctionDate modificationDate repaymentDate

save "dta/MCA_data_with_dates.dta", replace

****************************************************************************************************************************************************
/* Age of firms */
****************************************************************************************************************************************************

import delimited "raw\age_of_firms.txt", delimiter("|") clear

destring incorporation_year, force replace

rename co_code sa_finance1_cocode

save "dta/age_of_firms.dta", replace

****************************************************************************************************************************************************
/* Identifying risky (low quality) firms in 2007 */
****************************************************************************************************************************************************

import delimited "raw\firms_interest_coverage.txt", delimiter("|") clear

destring sa_total_income-sa_no_of_employees, force replace

tostring sa_finance1_year, force replace
gen day = substr(sa_finance1_year,7,2)
gen month = substr(sa_finance1_year,5,2)
gen year = substr(sa_finance1_year,1,4)

destring day-year, force replace

// Drop duplicate values such that there is only one firm-year observation
duplicates tag sa_finance1_cocode year, gen(x)
bysort sa_finance1_cocode: gen march = 1 if x>0 & month == 3
bysort sa_finance1_cocode: egen max_march = max(march) if x>0
drop if x>0 & max_march == 1 & march != 1
sort sa_finance1_cocode year month
bysort sa_finance1_cocode year: gen dup_n = _n if x > 0 & max_march != 1 
drop if dup_n > 1 & dup_n != .
sort sa_finance1_cocode year month day
bysort sa_finance1_cocode year: egen march_dup = sum(march) if x > 0 & max_march == 1 
bysort sa_finance1_cocode year: gen march_dup_n = _n if march_dup > 1 & march_dup != .
bysort sa_finance1_cocode year: gen march_dup_total_n = _N if march_dup > 1 & march_dup != .
drop if march_dup_n < march_dup_total_n & march_dup_n != .

drop x-march_dup_total_n

merge m:1 sa_finance1_cocode using "dta/industry_code.dta"
keep if _m==3
drop _m

merge m:1 sa_finance1_cocode using "dta/age_of_firms.dta"
drop if _m==2
drop _m

gen age = year - incorporation_year

tsset sa_finance1_cocode year, yearly

gen int_cov_ratio = sa_pbit/sa_interest_exp
drop if int_cov_ratio == .

gen risky_firm_zombie = 0
replace risky_firm_zombie = 1 if (int_cov_ratio < 1) & (L1.int_cov_ratio < 1) & (L2.int_cov_ratio < 1) & (age >= 10)

keep if year == 2007

bysort sa_finance1_cocode: egen avg_int_cov_ratio = mean(int_cov_ratio)
gen risky_firm1 = 0
replace risky_firm1 = 1 if avg_int_cov_ratio < 1

gen low_int_cov_ratio = 0
replace low_int_cov_ratio = 1 if int_cov_ratio < 1

bysort sa_finance1_cocode: egen risky_firm2 = max(low_int_cov_ratio)
bysort sa_finance1_cocode: egen risky_firm3 = min(low_int_cov_ratio)

duplicates drop sa_finance1_cocode, force

keep sa_finance1_cocode co_industry_name sa_company_name risky_firm1 risky_firm2 risky_firm3 avg_int_cov_ratio risky_firm_zombie

save "dta/firms_interest_coverage.dta", replace

****************************************************************************************************************************************************
/* Clean wilful defaulters data (from Nitin) */
****************************************************************************************************************************************************

import delimited "raw\WilfulDefaulters.csv", delimiter("|") clear

drop if v7 == ""

rename (v1 v2 v3 v4 v5 v6 v7 v8 v9 v10) (bank branch date_qrt borrower amount firmname sa_finance1_cocode firm_cin creditorcode date)

gen date2 = sa_finance1_cocode
gen creditorcode2 = firmname
gen sa_finance1_cocode2 = borrower

drop in 1

destring creditorcode sa_finance1_cocode creditorcode2 sa_finance1_cocode2, force replace

replace date = date2 if creditorcode2 != . & sa_finance1_cocode2 != .
replace creditorcode = creditorcode2 if creditorcode2 != . & sa_finance1_cocode2 != .
replace sa_finance1_cocode = sa_finance1_cocode2 if creditorcode2 != . & sa_finance1_cocode2 != .

merge m:1 creditorcode using "dta/public_sector_banks.dta" //self-made

keep if _m==3
drop _m

gen year = substr(date,1,4)
gen month = substr(date,6,2)

destring year month, force replace

gen quarter = .
replace quarter = 1 if month == 6
replace quarter = 2 if month == 9
replace quarter = 3 if month == 12
replace quarter = 4 if month == 3

replace year = year + 1 if month > 3

gen period_wilful = 4*(year - 2002) + quarter

bysort creditorcode sa_finance1_cocode year quarter: gen wilful_defaults = _N

duplicates drop creditorcode sa_finance1_cocode year quarter, force

save "dta/wilful_defaults_quarterly.dta", replace

****************************************************************************************************************************************************
/* Creating data having sanction period information of loans */
****************************************************************************************************************************************************

use "raw\LoansInMCA(2).dta", replace

keep if sanctionDate  >= td(01apr2003) & sanctionDate  <= td(30sep2007)

rename (creditorCode firmCode) (creditorcode firmcode)

merge m:1 creditorcode using "dta/public_sector_banks.dta" // dataset manually made

keep if _m==3
drop _m

gen period_sanction = .

replace period_sanction = 1 if sanctionDate >= td(01apr2001) & sanctionDate <= td(30jun2001)
replace period_sanction = 2 if sanctionDate >= td(01jul2001) & sanctionDate <= td(30sep2001)
replace period_sanction = 3 if sanctionDate >= td(01oct2001) & sanctionDate <= td(31dec2001)
replace period_sanction = 4 if sanctionDate >= td(01jan2002) & sanctionDate <= td(31mar2002)
replace period_sanction = 5 if sanctionDate >= td(01apr2002) & sanctionDate <= td(30jun2002)
replace period_sanction = 6 if sanctionDate >= td(01jul2002) & sanctionDate <= td(30sep2002)
replace period_sanction = 7 if sanctionDate >= td(01oct2002) & sanctionDate <= td(31dec2002)
replace period_sanction = 8 if sanctionDate >= td(01jan2003) & sanctionDate <= td(31mar2003)
replace period_sanction = 9 if sanctionDate >= td(01apr2003) & sanctionDate <= td(30jun2003)
replace period_sanction = 10 if sanctionDate >= td(01jul2003) & sanctionDate <= td(30sep2003)
replace period_sanction = 11 if sanctionDate >= td(01oct2003) & sanctionDate <= td(31dec2003)
replace period_sanction = 12 if sanctionDate >= td(01jan2004) & sanctionDate <= td(31mar2004)
replace period_sanction = 13 if sanctionDate >= td(01apr2004) & sanctionDate <= td(30jun2004)
replace period_sanction = 14 if sanctionDate >= td(01jul2004) & sanctionDate <= td(30sep2004)
replace period_sanction = 15 if sanctionDate >= td(01oct2004) & sanctionDate <= td(31dec2004)
replace period_sanction = 16 if sanctionDate >= td(01jan2005) & sanctionDate <= td(31mar2005)
replace period_sanction = 17 if sanctionDate >= td(01apr2005) & sanctionDate <= td(30jun2005)
replace period_sanction = 18 if sanctionDate >= td(01jul2005) & sanctionDate <= td(30sep2005)
replace period_sanction = 19 if sanctionDate >= td(01oct2005) & sanctionDate <= td(31dec2005)
replace period_sanction = 20 if sanctionDate >= td(01jan2006) & sanctionDate <= td(31mar2006)
replace period_sanction = 21 if sanctionDate >= td(01apr2006) & sanctionDate <= td(30jun2006)
replace period_sanction = 22 if sanctionDate >= td(01jul2006) & sanctionDate <= td(30sep2006)
replace period_sanction = 23 if sanctionDate >= td(01oct2006) & sanctionDate <= td(31dec2006)
replace period_sanction = 24 if sanctionDate >= td(01jan2007) & sanctionDate <= td(31mar2007)
replace period_sanction = 25 if sanctionDate >= td(01apr2007) & sanctionDate <= td(30jun2007)
replace period_sanction = 26 if sanctionDate >= td(01jul2007) & sanctionDate <= td(30sep2007)
replace period_sanction = 27 if sanctionDate >= td(01oct2007) & sanctionDate <= td(31dec2007)
replace period_sanction = 28 if sanctionDate >= td(01jan2008) & sanctionDate <= td(31mar2008)
replace period_sanction = 29 if sanctionDate >= td(01apr2008) & sanctionDate <= td(30jun2008)
replace period_sanction = 30 if sanctionDate >= td(01jul2008) & sanctionDate <= td(30sep2008)
replace period_sanction = 31 if sanctionDate >= td(01oct2008) & sanctionDate <= td(31dec2008)
replace period_sanction = 32 if sanctionDate >= td(01jan2009) & sanctionDate <= td(31mar2009)
replace period_sanction = 33 if sanctionDate >= td(01apr2009) & sanctionDate <= td(30jun2009)
replace period_sanction = 34 if sanctionDate >= td(01jul2009) & sanctionDate <= td(30sep2009)
replace period_sanction = 35 if sanctionDate >= td(01oct2009) & sanctionDate <= td(31dec2009)
replace period_sanction = 36 if sanctionDate >= td(01jan2010) & sanctionDate <= td(31mar2010)

gen year = int((period_sanction-1)/4)
replace year = year + 2002
gen quarter = mod(period_sanction,4)
replace quarter = 4 if quarter == 0

bysort creditorcode firmcode year quarter: egen loan_amount_sanctioned = sum(loanAmount)
bysort creditorcode firmcode year quarter: egen loan_given_sanctioned = sum(psb)

rename firmcode sa_finance1_cocode

duplicates drop creditorcode sa_finance1_cocode year quarter, force

drop if loan_amount_sanctioned == 0

save "dta/sanctioned_loans_outstanding.dta", replace

****************************************************************************************************************************************************
/* Creating data having repayment period information of loans */
****************************************************************************************************************************************************

use "raw\LoansInMCA(2).dta", replace

keep if sanctionDate  >= td(01apr2003) & sanctionDate  <= td(30sep2007)

rename (creditorCode firmCode) (creditorcode firmcode)

merge m:1 creditorcode using "dta/public_sector_banks.dta"

keep if _m==3
drop _m

gen period_repayment = .

replace period_repayment = 1 if repaymentDate >= td(01apr2001) & repaymentDate <= td(30jun2001)
replace period_repayment = 2 if repaymentDate >= td(01jul2001) & repaymentDate <= td(30sep2001)
replace period_repayment = 3 if repaymentDate >= td(01oct2001) & repaymentDate <= td(31dec2001)
replace period_repayment = 4 if repaymentDate >= td(01jan2002) & repaymentDate <= td(31mar2002)
replace period_repayment = 5 if repaymentDate >= td(01apr2002) & repaymentDate <= td(30jun2002)
replace period_repayment = 6 if repaymentDate >= td(01jul2002) & repaymentDate <= td(30sep2002)
replace period_repayment = 7 if repaymentDate >= td(01oct2002) & repaymentDate <= td(31dec2002)
replace period_repayment = 8 if repaymentDate >= td(01jan2003) & repaymentDate <= td(31mar2003)
replace period_repayment = 9 if repaymentDate >= td(01apr2003) & repaymentDate <= td(30jun2003)
replace period_repayment = 10 if repaymentDate >= td(01jul2003) & repaymentDate <= td(30sep2003)
replace period_repayment = 11 if repaymentDate >= td(01oct2003) & repaymentDate <= td(31dec2003)
replace period_repayment = 12 if repaymentDate >= td(01jan2004) & repaymentDate <= td(31mar2004)
replace period_repayment = 13 if repaymentDate >= td(01apr2004) & repaymentDate <= td(30jun2004)
replace period_repayment = 14 if repaymentDate >= td(01jul2004) & repaymentDate <= td(30sep2004)
replace period_repayment = 15 if repaymentDate >= td(01oct2004) & repaymentDate <= td(31dec2004)
replace period_repayment = 16 if repaymentDate >= td(01jan2005) & repaymentDate <= td(31mar2005)
replace period_repayment = 17 if repaymentDate >= td(01apr2005) & repaymentDate <= td(30jun2005)
replace period_repayment = 18 if repaymentDate >= td(01jul2005) & repaymentDate <= td(30sep2005)
replace period_repayment = 19 if repaymentDate >= td(01oct2005) & repaymentDate <= td(31dec2005)
replace period_repayment = 20 if repaymentDate >= td(01jan2006) & repaymentDate <= td(31mar2006)
replace period_repayment = 21 if repaymentDate >= td(01apr2006) & repaymentDate <= td(30jun2006)
replace period_repayment = 22 if repaymentDate >= td(01jul2006) & repaymentDate <= td(30sep2006)
replace period_repayment = 23 if repaymentDate >= td(01oct2006) & repaymentDate <= td(31dec2006)
replace period_repayment = 24 if repaymentDate >= td(01jan2007) & repaymentDate <= td(31mar2007)
replace period_repayment = 25 if repaymentDate >= td(01apr2007) & repaymentDate <= td(30jun2007)
replace period_repayment = 26 if repaymentDate >= td(01jul2007) & repaymentDate <= td(30sep2007)
replace period_repayment = 27 if repaymentDate >= td(01oct2007) & repaymentDate <= td(31dec2007)
replace period_repayment = 28 if repaymentDate >= td(01jan2008) & repaymentDate <= td(31mar2008)
replace period_repayment = 29 if repaymentDate >= td(01apr2008) & repaymentDate <= td(30jun2008)
replace period_repayment = 30 if repaymentDate >= td(01jul2008) & repaymentDate <= td(30sep2008)
replace period_repayment = 31 if repaymentDate >= td(01oct2008) & repaymentDate <= td(31dec2008)
replace period_repayment = 32 if repaymentDate >= td(01jan2009) & repaymentDate <= td(31mar2009)
replace period_repayment = 33 if repaymentDate >= td(01apr2009) & repaymentDate <= td(30jun2009)
replace period_repayment = 34 if repaymentDate >= td(01jul2009) & repaymentDate <= td(30sep2009)
replace period_repayment = 35 if repaymentDate >= td(01oct2009) & repaymentDate <= td(31dec2009)
replace period_repayment = 36 if repaymentDate >= td(01jan2010) & repaymentDate <= td(31mar2010)

gen year = int((period_repayment-1)/4)
replace year = year + 2002
gen quarter = mod(period_repayment,4)
replace quarter = 4 if quarter == 0

bysort creditorcode firmcode year quarter: egen loan_amount_repayment = sum(loanAmount)
bysort creditorcode firmcode year quarter: egen loan_given_repayment = sum(psb)

rename firmcode sa_finance1_cocode

duplicates drop creditorcode sa_finance1_cocode year quarter, force

save "dta/repaid_loans_outstanding.dta", replace

****************************************************************************************************************************************************
/* Creating restructuring data (modifications) */
****************************************************************************************************************************************************

use "raw\LoansInMCA(2).dta", replace

keep if sanctionDate  >= td(01apr2003) & sanctionDate  <= td(30sep2007)
keep if modificationDate  >= td(01apr2003) & modificationDate  <= td(31mar2010)

rename (creditorCode firmCode) (creditorcode firmcode)

merge m:1 creditorcode using "dta/public_sector_banks.dta"

keep if _m==3
drop _m

gen period = .

replace period = 1 if modificationDate >= td(01apr2001) & modificationDate <= td(30jun2001)
replace period = 2 if modificationDate >= td(01jul2001) & modificationDate <= td(30sep2001)
replace period = 3 if modificationDate >= td(01oct2001) & modificationDate <= td(31dec2001)
replace period = 4 if modificationDate >= td(01jan2002) & modificationDate <= td(31mar2002)
replace period = 5 if modificationDate >= td(01apr2002) & modificationDate <= td(30jun2002)
replace period = 6 if modificationDate >= td(01jul2002) & modificationDate <= td(30sep2002)
replace period = 7 if modificationDate >= td(01oct2002) & modificationDate <= td(31dec2002)
replace period = 8 if modificationDate >= td(01jan2003) & modificationDate <= td(31mar2003)
replace period = 9 if modificationDate >= td(01apr2003) & modificationDate <= td(30jun2003)
replace period = 10 if modificationDate >= td(01jul2003) & modificationDate <= td(30sep2003)
replace period = 11 if modificationDate >= td(01oct2003) & modificationDate <= td(31dec2003)
replace period = 12 if modificationDate >= td(01jan2004) & modificationDate <= td(31mar2004)
replace period = 13 if modificationDate >= td(01apr2004) & modificationDate <= td(30jun2004)
replace period = 14 if modificationDate >= td(01jul2004) & modificationDate <= td(30sep2004)
replace period = 15 if modificationDate >= td(01oct2004) & modificationDate <= td(31dec2004)
replace period = 16 if modificationDate >= td(01jan2005) & modificationDate <= td(31mar2005)
replace period = 17 if modificationDate >= td(01apr2005) & modificationDate <= td(30jun2005)
replace period = 18 if modificationDate >= td(01jul2005) & modificationDate <= td(30sep2005)
replace period = 19 if modificationDate >= td(01oct2005) & modificationDate <= td(31dec2005)
replace period = 20 if modificationDate >= td(01jan2006) & modificationDate <= td(31mar2006)
replace period = 21 if modificationDate >= td(01apr2006) & modificationDate <= td(30jun2006)
replace period = 22 if modificationDate >= td(01jul2006) & modificationDate <= td(30sep2006)
replace period = 23 if modificationDate >= td(01oct2006) & modificationDate <= td(31dec2006)
replace period = 24 if modificationDate >= td(01jan2007) & modificationDate <= td(31mar2007)
replace period = 25 if modificationDate >= td(01apr2007) & modificationDate <= td(30jun2007)
replace period = 26 if modificationDate >= td(01jul2007) & modificationDate <= td(30sep2007)
replace period = 27 if modificationDate >= td(01oct2007) & modificationDate <= td(31dec2007)
replace period = 28 if modificationDate >= td(01jan2008) & modificationDate <= td(31mar2008)
replace period = 29 if modificationDate >= td(01apr2008) & modificationDate <= td(30jun2008)
replace period = 30 if modificationDate >= td(01jul2008) & modificationDate <= td(30sep2008)
replace period = 31 if modificationDate >= td(01oct2008) & modificationDate <= td(31dec2008)
replace period = 32 if modificationDate >= td(01jan2009) & modificationDate <= td(31mar2009)
replace period = 33 if modificationDate >= td(01apr2009) & modificationDate <= td(30jun2009)
replace period = 34 if modificationDate >= td(01jul2009) & modificationDate <= td(30sep2009)
replace period = 35 if modificationDate >= td(01oct2009) & modificationDate <= td(31dec2009)
replace period = 36 if modificationDate >= td(01jan2010) & modificationDate <= td(31mar2010)

gen period_sanction = .

replace period_sanction = 1 if sanctionDate >= td(01apr2001) & sanctionDate <= td(30jun2001)
replace period_sanction = 2 if sanctionDate >= td(01jul2001) & sanctionDate <= td(30sep2001)
replace period_sanction = 3 if sanctionDate >= td(01oct2001) & sanctionDate <= td(31dec2001)
replace period_sanction = 4 if sanctionDate >= td(01jan2002) & sanctionDate <= td(31mar2002)
replace period_sanction = 5 if sanctionDate >= td(01apr2002) & sanctionDate <= td(30jun2002)
replace period_sanction = 6 if sanctionDate >= td(01jul2002) & sanctionDate <= td(30sep2002)
replace period_sanction = 7 if sanctionDate >= td(01oct2002) & sanctionDate <= td(31dec2002)
replace period_sanction = 8 if sanctionDate >= td(01jan2003) & sanctionDate <= td(31mar2003)
replace period_sanction = 9 if sanctionDate >= td(01apr2003) & sanctionDate <= td(30jun2003)
replace period_sanction = 10 if sanctionDate >= td(01jul2003) & sanctionDate <= td(30sep2003)
replace period_sanction = 11 if sanctionDate >= td(01oct2003) & sanctionDate <= td(31dec2003)
replace period_sanction = 12 if sanctionDate >= td(01jan2004) & sanctionDate <= td(31mar2004)
replace period_sanction = 13 if sanctionDate >= td(01apr2004) & sanctionDate <= td(30jun2004)
replace period_sanction = 14 if sanctionDate >= td(01jul2004) & sanctionDate <= td(30sep2004)
replace period_sanction = 15 if sanctionDate >= td(01oct2004) & sanctionDate <= td(31dec2004)
replace period_sanction = 16 if sanctionDate >= td(01jan2005) & sanctionDate <= td(31mar2005)
replace period_sanction = 17 if sanctionDate >= td(01apr2005) & sanctionDate <= td(30jun2005)
replace period_sanction = 18 if sanctionDate >= td(01jul2005) & sanctionDate <= td(30sep2005)
replace period_sanction = 19 if sanctionDate >= td(01oct2005) & sanctionDate <= td(31dec2005)
replace period_sanction = 20 if sanctionDate >= td(01jan2006) & sanctionDate <= td(31mar2006)
replace period_sanction = 21 if sanctionDate >= td(01apr2006) & sanctionDate <= td(30jun2006)
replace period_sanction = 22 if sanctionDate >= td(01jul2006) & sanctionDate <= td(30sep2006)
replace period_sanction = 23 if sanctionDate >= td(01oct2006) & sanctionDate <= td(31dec2006)
replace period_sanction = 24 if sanctionDate >= td(01jan2007) & sanctionDate <= td(31mar2007)
replace period_sanction = 25 if sanctionDate >= td(01apr2007) & sanctionDate <= td(30jun2007)
replace period_sanction = 26 if sanctionDate >= td(01jul2007) & sanctionDate <= td(30sep2007)
replace period_sanction = 27 if sanctionDate >= td(01oct2007) & sanctionDate <= td(31dec2007)
replace period_sanction = 28 if sanctionDate >= td(01jan2008) & sanctionDate <= td(31mar2008)
replace period_sanction = 29 if sanctionDate >= td(01apr2008) & sanctionDate <= td(30jun2008)
replace period_sanction = 30 if sanctionDate >= td(01jul2008) & sanctionDate <= td(30sep2008)
replace period_sanction = 31 if sanctionDate >= td(01oct2008) & sanctionDate <= td(31dec2008)
replace period_sanction = 32 if sanctionDate >= td(01jan2009) & sanctionDate <= td(31mar2009)
replace period_sanction = 33 if sanctionDate >= td(01apr2009) & sanctionDate <= td(30jun2009)
replace period_sanction = 34 if sanctionDate >= td(01jul2009) & sanctionDate <= td(30sep2009)
replace period_sanction = 35 if sanctionDate >= td(01oct2009) & sanctionDate <= td(31dec2009)
replace period_sanction = 36 if sanctionDate >= td(01jan2010) & sanctionDate <= td(31mar2010)

gen year = int((period-1)/4)
replace year = year + 2002
gen quarter = mod(period,4)
replace quarter = 4 if quarter == 0

bysort creditorcode firmcode year quarter: gen num_of_modifications = _N

gen same_period_modification = 0
replace same_period_modification = 1 if period == period_sanction

bysort creditorcode firmcode year quarter: egen num_same_modifications = sum(same_period_modification)
bysort creditorcode firmcode year quarter: egen loan_amount_modified = sum(loanAmount)

gen num_of_modifications2 = num_of_modifications - num_same_modifications

duplicates drop creditorcode firmcode year quarter, force

rename firmcode sa_finance1_cocode

keep creditorcode sa_finance1_cocode year quarter period num_of_modifications num_of_modifications2 loanAmount loan_amount_modified

save "dta/restructuring_psb_final.dta", replace

****************************************************************************************************************************************************
/* Capital adequacy ratio information of firms (from prowess)*/
****************************************************************************************************************************************************

import delimited "raw\capital_adequacy_ratio.txt", delimiter("|") clear

drop v49

destring sa_total_income-sa_bsl3_net_npas_to_net_adv_pct, force replace

tostring sa_finance1_year, force replace
gen day = substr(sa_finance1_year,7,2)
gen month = substr(sa_finance1_year,5,2)
gen year = substr(sa_finance1_year,1,4)

destring day-year, force replace

duplicates tag sa_finance1_cocode year, gen(x)
bysort sa_finance1_cocode: gen march = 1 if x>0 & month == 3
bysort sa_finance1_cocode: egen max_march = max(march) if x>0
drop if x>0 & max_march == 1 & march != 1
sort sa_finance1_cocode year month
bysort sa_finance1_cocode year: gen dup_n = _n if x > 0 & max_march != 1 
drop if dup_n > 1 & dup_n != .
sort sa_finance1_cocode year month day
bysort sa_finance1_cocode year: egen march_dup = sum(march) if x > 0 & max_march == 1 
bysort sa_finance1_cocode year: gen march_dup_n = _n if march_dup > 1 & march_dup != .
bysort sa_finance1_cocode year: gen march_dup_total_n = _N if march_dup > 1 & march_dup != .
drop if march_dup_n < march_dup_total_n & march_dup_n != .

drop x-march_dup_total_n

rename sa_finance1_cocode creditorcode

save "dta/capital_adequacy_ratio.dta", replace

****************************************************************************************************************************************************
/* Making CEO change data into quaterly data */
****************************************************************************************************************************************************

use "raw\CEO_Tenure.dta", clear

reshape long CEO_, i(creditorCode) j(observation)
rename (creditorCode observation CEO_ sa_company_name) (creditorcode obs ceo psbname)

keep if ceo >= td(01oct2007) & ceo <= td(30jun2009)

bysort psbname: egen min_ceo = min(ceo)

format min_ceo %td

gen period_ceo = .
replace period_ceo = 1 if min_ceo >= td(01apr2001) & min_ceo <= td(30jun2001)
replace period_ceo = 2 if min_ceo >= td(01jul2001) & min_ceo <= td(30sep2001)
replace period_ceo = 3 if min_ceo >= td(01oct2001) & min_ceo <= td(31dec2001)
replace period_ceo = 4 if min_ceo >= td(01jan2002) & min_ceo <= td(31mar2002)
replace period_ceo = 5 if min_ceo >= td(01apr2002) & min_ceo <= td(30jun2002)
replace period_ceo = 6 if min_ceo >= td(01jul2002) & min_ceo <= td(30sep2002)
replace period_ceo = 7 if min_ceo >= td(01oct2002) & min_ceo <= td(31dec2002)
replace period_ceo = 8 if min_ceo >= td(01jan2003) & min_ceo <= td(31mar2003)
replace period_ceo = 9 if min_ceo >= td(01apr2003) & min_ceo <= td(30jun2003)
replace period_ceo = 10 if min_ceo >= td(01jul2003) & min_ceo <= td(30sep2003)
replace period_ceo = 11 if min_ceo >= td(01oct2003) & min_ceo <= td(31dec2003)
replace period_ceo = 12 if min_ceo >= td(01jan2004) & min_ceo <= td(31mar2004)
replace period_ceo = 13 if min_ceo >= td(01apr2004) & min_ceo <= td(30jun2004)
replace period_ceo = 14 if min_ceo >= td(01jul2004) & min_ceo <= td(30sep2004)
replace period_ceo = 15 if min_ceo >= td(01oct2004) & min_ceo <= td(31dec2004)
replace period_ceo = 16 if min_ceo >= td(01jan2005) & min_ceo <= td(31mar2005)
replace period_ceo = 17 if min_ceo >= td(01apr2005) & min_ceo <= td(30jun2005)
replace period_ceo = 18 if min_ceo >= td(01jul2005) & min_ceo <= td(30sep2005)
replace period_ceo = 19 if min_ceo >= td(01oct2005) & min_ceo <= td(31dec2005)
replace period_ceo = 20 if min_ceo >= td(01jan2006) & min_ceo <= td(31mar2006)
replace period_ceo = 21 if min_ceo >= td(01apr2006) & min_ceo <= td(30jun2006)
replace period_ceo = 22 if min_ceo >= td(01jul2006) & min_ceo <= td(30sep2006)
replace period_ceo = 23 if min_ceo >= td(01oct2006) & min_ceo <= td(31dec2006)
replace period_ceo = 24 if min_ceo >= td(01jan2007) & min_ceo <= td(31mar2007)
replace period_ceo = 25 if min_ceo >= td(01apr2007) & min_ceo <= td(30jun2007)
replace period_ceo = 26 if min_ceo >= td(01jul2007) & min_ceo <= td(30sep2007)
replace period_ceo = 27 if min_ceo >= td(01oct2007) & min_ceo <= td(31dec2007)
replace period_ceo = 28 if min_ceo >= td(01jan2008) & min_ceo <= td(31mar2008)
replace period_ceo = 29 if min_ceo >= td(01apr2008) & min_ceo <= td(30jun2008)
replace period_ceo = 30 if min_ceo >= td(01jul2008) & min_ceo <= td(30sep2008)
replace period_ceo = 31 if min_ceo >= td(01oct2008) & min_ceo <= td(31dec2008)
replace period_ceo = 32 if min_ceo >= td(01jan2009) & min_ceo <= td(31mar2009)
replace period_ceo = 33 if min_ceo >= td(01apr2009) & min_ceo <= td(30jun2009)
replace period_ceo = 34 if min_ceo >= td(01jul2009) & min_ceo <= td(30sep2009)
replace period_ceo = 35 if min_ceo >= td(01oct2009) & min_ceo <= td(31dec2009)
replace period_ceo = 36 if min_ceo >= td(01jan2010) & min_ceo <= td(31mar2010)

save "dta/ceo_change_quarterly.dta", replace

****************************************************************************************************************************************************
/* Quarterly data filtered for public sector banks (from prowess) */
****************************************************************************************************************************************************

import delimited "raw\bank_info.txt", delimiter("|") clear 

tostring siq_ntrm_date, force replace

gen year = substr(siq_ntrm_date,1,4)
gen month = substr(siq_ntrm_date,5,2)

destring siq_ntrm_total_inc-month, force replace

gen quarter = .
replace quarter = 1 if month == 6
replace quarter = 2 if month == 9
replace quarter = 3 if month == 12
replace quarter = 4 if month == 3

replace year = year + 1 if month > 3

gen period_new = 4*(year - 2002) + quarter

rename siq_interim_cocode creditorcode

merge m:1 creditorcode using "dta/public_sector_banks.dta" //self - made

keep if _m==3
drop _m

merge m:1 creditorcode using "dta/ceo_change_quarterly.dta"

replace period_ceo = 10000 if _m==1

drop if _m==2 
drop _m

gen treat1 = 0
replace treat1 = 1 if period_new >= period_ceo_ext

bysort creditorcode: egen treat1_all = max(treat1)

gen treat1_all2 = treat1_all
replace treat1_all2 = . if treat1 == 1

gen pat_income = siq_ntrm_after_tax_profit_loss_c/siq_ntrm_total_inc
gen log_income = log(1 + siq_ntrm_total_inc)
gen log_pat = log(1 + siq_ntrm_after_tax_profit_loss_c)

drop pat_income log_income log_pat

save "dta/bank_info.dta", replace

****************************************************************************************************************************************************
/* Firm-Year Controls */
****************************************************************************************************************************************************

import delimited "raw\firms_interest_coverage.txt", delimiter("|") clear

destring sa_total_income-sa_debt_equity, force replace

tostring sa_finance1_year, force replace
gen day = substr(sa_finance1_year,7,2)
gen month = substr(sa_finance1_year,5,2)
gen year = substr(sa_finance1_year,1,4)

destring day-year, force replace

duplicates tag sa_finance1_cocode year, gen(x)
bysort sa_finance1_cocode: gen march = 1 if x>0 & month == 3
bysort sa_finance1_cocode: egen max_march = max(march) if x>0
drop if x>0 & max_march == 1 & march != 1
sort sa_finance1_cocode year month
bysort sa_finance1_cocode year: gen dup_n = _n if x > 0 & max_march != 1 
drop if dup_n > 1 & dup_n != .
sort sa_finance1_cocode year month day
bysort sa_finance1_cocode year: egen march_dup = sum(march) if x > 0 & max_march == 1 
bysort sa_finance1_cocode year: gen march_dup_n = _n if march_dup > 1 & march_dup != .
bysort sa_finance1_cocode year: gen march_dup_total_n = _N if march_dup > 1 & march_dup != .
drop if march_dup_n < march_dup_total_n & march_dup_n != .

drop x-march_dup_total_n

keep sa_finance1_cocode year sa_total_income sa_sales sa_pat sa_pbdita sa_pbt sa_pbit sa_total_liabilities sa_total_capital sa_net_worth sa_debt sa_total_assets sa_net_ppe sa_plant

replace year = year + 1

save "dta/firm_year_controls.dta", replace

****************************************************************************************************************************************************
/* Keeping firm-bank pairs for which loans were sanctioned between 2001 and 2010 as our focus is around this period */
/* Different dataset according to type of bank/financial companies */
****************************************************************************************************************************************************

use "dta/MCA_data_with_dates.dta", replace

keep if sanctionDate >= td(01apr2001) & sanctionDate <= td(31mar2010)

duplicates drop creditorCode firmCode, force

rename (creditorCode firmCode) (creditorcode firmcode)

merge m:1 creditorcode using "dta/public_sector_banks.dta"

keep if _m==3
drop _m

keep creditorName creditorcode firmcode firmName psb_name

save "dta/MCA_data_psb.dta", replace

****************************************************************************************************************************************************
/* Creating balanced panel from firm-bank pair (quarter)*/
****************************************************************************************************************************************************

use "dta/MCA_data_psb.dta", replace
append using "dta/MCA_data_psb.dta"
append using "dta/MCA_data_psb.dta"
append using "dta/MCA_data_psb.dta"

bysort creditorcode firmcode: gen quarter = _n

save "dta/MCA_data_psb_only_quarter.dta", replace

****************************************************************************************************************************************************
/* Creating balanced panel from firm-bank pair (quarter-year)*/
****************************************************************************************************************************************************

use "dta/MCA_data_psb_only_quarter.dta", replace
append using "dta/MCA_data_psb_only_quarter.dta"
append using "dta/MCA_data_psb_only_quarter.dta"
append using "dta/MCA_data_psb_only_quarter.dta"
append using "dta/MCA_data_psb_only_quarter.dta"
append using "dta/MCA_data_psb_only_quarter.dta"
append using "dta/MCA_data_psb_only_quarter.dta"
append using "dta/MCA_data_psb_only_quarter.dta"
append using "dta/MCA_data_psb_only_quarter.dta"

bysort creditorcode firmcode quarter: gen year = _n

replace year = year + 2001

save "dta/MCA_data_psb_year_quarter_blank.dta", replace

****************************************************************************************************************************************************
/* Giving quarter-year values to the original data */
****************************************************************************************************************************************************

use "dta/MCA_data_with_dates.dta", replace

keep if sanctionDate >= td(01apr2001) & sanctionDate <= td(31mar2010)

rename (creditorCode firmCode) (creditorcode firmcode)

merge m:1 creditorcode using "dta/public_sector_banks.dta"

keep if _m==3
drop _m

gen period = .

replace period = 1 if sanctionDate >= td(01apr2001) & sanctionDate <= td(30jun2001)
replace period = 2 if sanctionDate >= td(01jul2001) & sanctionDate <= td(30sep2001)
replace period = 3 if sanctionDate >= td(01oct2001) & sanctionDate <= td(31dec2001)
replace period = 4 if sanctionDate >= td(01jan2002) & sanctionDate <= td(31mar2002)
replace period = 5 if sanctionDate >= td(01apr2002) & sanctionDate <= td(30jun2002)
replace period = 6 if sanctionDate >= td(01jul2002) & sanctionDate <= td(30sep2002)
replace period = 7 if sanctionDate >= td(01oct2002) & sanctionDate <= td(31dec2002)
replace period = 8 if sanctionDate >= td(01jan2003) & sanctionDate <= td(31mar2003)
replace period = 9 if sanctionDate >= td(01apr2003) & sanctionDate <= td(30jun2003)
replace period = 10 if sanctionDate >= td(01jul2003) & sanctionDate <= td(30sep2003)
replace period = 11 if sanctionDate >= td(01oct2003) & sanctionDate <= td(31dec2003)
replace period = 12 if sanctionDate >= td(01jan2004) & sanctionDate <= td(31mar2004)
replace period = 13 if sanctionDate >= td(01apr2004) & sanctionDate <= td(30jun2004)
replace period = 14 if sanctionDate >= td(01jul2004) & sanctionDate <= td(30sep2004)
replace period = 15 if sanctionDate >= td(01oct2004) & sanctionDate <= td(31dec2004)
replace period = 16 if sanctionDate >= td(01jan2005) & sanctionDate <= td(31mar2005)
replace period = 17 if sanctionDate >= td(01apr2005) & sanctionDate <= td(30jun2005)
replace period = 18 if sanctionDate >= td(01jul2005) & sanctionDate <= td(30sep2005)
replace period = 19 if sanctionDate >= td(01oct2005) & sanctionDate <= td(31dec2005)
replace period = 20 if sanctionDate >= td(01jan2006) & sanctionDate <= td(31mar2006)
replace period = 21 if sanctionDate >= td(01apr2006) & sanctionDate <= td(30jun2006)
replace period = 22 if sanctionDate >= td(01jul2006) & sanctionDate <= td(30sep2006)
replace period = 23 if sanctionDate >= td(01oct2006) & sanctionDate <= td(31dec2006)
replace period = 24 if sanctionDate >= td(01jan2007) & sanctionDate <= td(31mar2007)
replace period = 25 if sanctionDate >= td(01apr2007) & sanctionDate <= td(30jun2007)
replace period = 26 if sanctionDate >= td(01jul2007) & sanctionDate <= td(30sep2007)
replace period = 27 if sanctionDate >= td(01oct2007) & sanctionDate <= td(31dec2007)
replace period = 28 if sanctionDate >= td(01jan2008) & sanctionDate <= td(31mar2008)
replace period = 29 if sanctionDate >= td(01apr2008) & sanctionDate <= td(30jun2008)
replace period = 30 if sanctionDate >= td(01jul2008) & sanctionDate <= td(30sep2008)
replace period = 31 if sanctionDate >= td(01oct2008) & sanctionDate <= td(31dec2008)
replace period = 32 if sanctionDate >= td(01jan2009) & sanctionDate <= td(31mar2009)
replace period = 33 if sanctionDate >= td(01apr2009) & sanctionDate <= td(30jun2009)
replace period = 34 if sanctionDate >= td(01jul2009) & sanctionDate <= td(30sep2009)
replace period = 35 if sanctionDate >= td(01oct2009) & sanctionDate <= td(31dec2009)
replace period = 36 if sanctionDate >= td(01jan2010) & sanctionDate <= td(31mar2010)

gen period_repayment = .

replace period_repayment = 1 if repaymentDate >= td(01apr2001) & repaymentDate <= td(30jun2001)
replace period_repayment = 2 if repaymentDate >= td(01jul2001) & repaymentDate <= td(30sep2001)
replace period_repayment = 3 if repaymentDate >= td(01oct2001) & repaymentDate <= td(31dec2001)
replace period_repayment = 4 if repaymentDate >= td(01jan2002) & repaymentDate <= td(31mar2002)
replace period_repayment = 5 if repaymentDate >= td(01apr2002) & repaymentDate <= td(30jun2002)
replace period_repayment = 6 if repaymentDate >= td(01jul2002) & repaymentDate <= td(30sep2002)
replace period_repayment = 7 if repaymentDate >= td(01oct2002) & repaymentDate <= td(31dec2002)
replace period_repayment = 8 if repaymentDate >= td(01jan2003) & repaymentDate <= td(31mar2003)
replace period_repayment = 9 if repaymentDate >= td(01apr2003) & repaymentDate <= td(30jun2003)
replace period_repayment = 10 if repaymentDate >= td(01jul2003) & repaymentDate <= td(30sep2003)
replace period_repayment = 11 if repaymentDate >= td(01oct2003) & repaymentDate <= td(31dec2003)
replace period_repayment = 12 if repaymentDate >= td(01jan2004) & repaymentDate <= td(31mar2004)
replace period_repayment = 13 if repaymentDate >= td(01apr2004) & repaymentDate <= td(30jun2004)
replace period_repayment = 14 if repaymentDate >= td(01jul2004) & repaymentDate <= td(30sep2004)
replace period_repayment = 15 if repaymentDate >= td(01oct2004) & repaymentDate <= td(31dec2004)
replace period_repayment = 16 if repaymentDate >= td(01jan2005) & repaymentDate <= td(31mar2005)
replace period_repayment = 17 if repaymentDate >= td(01apr2005) & repaymentDate <= td(30jun2005)
replace period_repayment = 18 if repaymentDate >= td(01jul2005) & repaymentDate <= td(30sep2005)
replace period_repayment = 19 if repaymentDate >= td(01oct2005) & repaymentDate <= td(31dec2005)
replace period_repayment = 20 if repaymentDate >= td(01jan2006) & repaymentDate <= td(31mar2006)
replace period_repayment = 21 if repaymentDate >= td(01apr2006) & repaymentDate <= td(30jun2006)
replace period_repayment = 22 if repaymentDate >= td(01jul2006) & repaymentDate <= td(30sep2006)
replace period_repayment = 23 if repaymentDate >= td(01oct2006) & repaymentDate <= td(31dec2006)
replace period_repayment = 24 if repaymentDate >= td(01jan2007) & repaymentDate <= td(31mar2007)
replace period_repayment = 25 if repaymentDate >= td(01apr2007) & repaymentDate <= td(30jun2007)
replace period_repayment = 26 if repaymentDate >= td(01jul2007) & repaymentDate <= td(30sep2007)
replace period_repayment = 27 if repaymentDate >= td(01oct2007) & repaymentDate <= td(31dec2007)
replace period_repayment = 28 if repaymentDate >= td(01jan2008) & repaymentDate <= td(31mar2008)
replace period_repayment = 29 if repaymentDate >= td(01apr2008) & repaymentDate <= td(30jun2008)
replace period_repayment = 30 if repaymentDate >= td(01jul2008) & repaymentDate <= td(30sep2008)
replace period_repayment = 31 if repaymentDate >= td(01oct2008) & repaymentDate <= td(31dec2008)
replace period_repayment = 32 if repaymentDate >= td(01jan2009) & repaymentDate <= td(31mar2009)
replace period_repayment = 33 if repaymentDate >= td(01apr2009) & repaymentDate <= td(30jun2009)
replace period_repayment = 34 if repaymentDate >= td(01jul2009) & repaymentDate <= td(30sep2009)
replace period_repayment = 35 if repaymentDate >= td(01oct2009) & repaymentDate <= td(31dec2009)
replace period_repayment = 36 if repaymentDate >= td(01jan2010) & repaymentDate <= td(31mar2010)

gen year = int((period-1)/4)
replace year = year + 2002
gen quarter = mod(period,4)
replace quarter = 4 if quarter == 0

gen zero_loan_amount = 0
replace zero_loan_amount = 1 if loanAmount == 0

bysort creditorcode firmcode year quarter: egen total_loan_amount = sum(loanAmount)
bysort creditorcode firmcode year quarter: egen total_loan_given = sum(psb)

duplicates drop creditorcode firmcode year quarter, force

save "dta/MCA_data_psb_quarter_included.dta", replace

****************************************************************************************************************************************************
/* Merging the originial data with balanced panel data since we have quarter-year values in original data */
****************************************************************************************************************************************************

use "dta/MCA_data_psb_year_quarter_blank.dta", replace
merge 1:1 creditorcode firmcode year quarter using "dta/MCA_data_psb_quarter_included.dta"

bysort creditorcode firmcode: egen first_sanction_period = min(period)
bysort creditorcode firmcode: egen last_repayment_period = max(period_repayment)

replace total_loan_amount = 0 if _m==1
replace total_loan_given = 0 if _m==1

drop _m

gen period_new = 4*(year-2002) + quarter

rename firmcode sa_finance1_cocode

save "dta/MCA_data_psb_year_quarter_final.dta", replace

****************************************************************************************************************************************************
/* Getting firm-bank relationships that existed before the crisis */
****************************************************************************************************************************************************

use "dta/MCA_data_psb_year_quarter_final.dta", replace

keep if period_new >= 9 & period_new <= 26
keep if total_loan_given != 0
duplicates drop sa_finance1_cocode creditorcode, force

keep sa_finance1_cocode creditorcode 

distinct sa_finance1_cocode
distinct creditorcode

save "dta/prev_yrs_loan_firms_quarterly.dta", replace

****************************************************************************************************************************************************
/* Keeping ony those firm-bank pairs that had existing relationship in the merged data */
****************************************************************************************************************************************************

use "dta/MCA_data_psb_year_quarter_final.dta", replace

merge m:1 sa_finance1_cocode creditorcode using "dta/prev_yrs_loan_firms_quarterly.dta"

keep if _m==3
drop _m

merge m:1 creditorcode using "dta/ceo_change_quarterly.dta"

replace period_ceo = 10000 if _m==1

drop if _m==2 
drop _m

gen treat1 = 0
replace treat1 = 1 if period_new >= period_ceo

gen treat2 = 0
replace treat2 = 1 if period_new > period_ceo

bysort creditorcode: egen sum_treat1 = sum(treat1)

count if period_new >= 28 & period_new <= 33

merge m:1 sa_finance1_cocode using "dta/firms_interest_coverage.dta"
keep if _m==3
drop _m

distinct sa_finance1_cocode if period_new >= 28 & period_new <= 33 & risky_firm1 == 1
distinct sa_finance1_cocode if period_new >= 28 & period_new <= 33 & risky_firm1 == 0

count if period_new >= 28 & period_new <= 33

merge m:1 sa_finance1_cocode using "dta/industry.dta" // self-made
drop if _m==2
drop _m

gen int_treat1_risky1 = treat1*risky_firm1
gen int_treat1_risky_zombie = treat1*risky_firm_zombie
gen int_treat1_risky2 = treat1*risky_firm2
gen int_treat1_risky3 = treat1*risky_firm3
gen int_treat2_risky1 = treat2*risky_firm1
gen int_treat2_risky2 = treat2*risky_firm2
gen int_treat2_risky3 = treat2*risky_firm3

gen total_loan_given_dummy = 0
replace total_loan_given_dummy = 1 if total_loan_given > 0

egen firm_year = group(year sa_finance1_cocode)
egen firm_period = group(period_new  sa_finance1_cocode)
gen nic_prod_code_2 = substr(nic_prod_code,1,2)
gen nic_prod_code_3 = substr(nic_prod_code,1,3)

bysort creditorcode: egen first_period_ceo = min(period_new) if treat1 == 1
bysort creditorcode: egen min_first_period_ceo = min(first_period_ceo)

gen dist_from_ceo_change = period_new - min_first_period

gen dummy_pre1 = 0
replace dummy_pre1 = 1 if dist_from_ceo_change == -1
gen dummy_pre2 = 0
replace dummy_pre2 = 1 if dist_from_ceo_change == -2
gen dummy_pre3 = 0
replace dummy_pre3 = 1 if dist_from_ceo_change == -3
gen dummy_pre4 = 0
replace dummy_pre4 = 1 if dist_from_ceo_change == -4
gen dummy_pre5 = 0
replace dummy_pre5 = 1 if dist_from_ceo_change == -5
gen dummy_post = 0
replace dummy_post = 1 if dist_from_ceo_change >= 0 & dist_from_ceo_change != .

gen dummy_pre1_risky1 = dummy_pre1*risky_firm1
gen dummy_pre2_risky1 = dummy_pre2*risky_firm1
gen dummy_pre3_risky1 = dummy_pre3*risky_firm1
gen dummy_pre4_risky1 = dummy_pre4*risky_firm1
gen dummy_pre5_risky1 = dummy_pre5*risky_firm1
gen dummy_post_risky1 = dummy_post*risky_firm1

gen dummy_pre1_risky_zombie = dummy_pre1*risky_firm_zombie
gen dummy_pre2_risky_zombie = dummy_pre2*risky_firm_zombie
gen dummy_pre3_risky_zombie = dummy_pre3*risky_firm_zombie
gen dummy_pre4_risky_zombie = dummy_pre4*risky_firm_zombie
gen dummy_pre5_risky_zombie = dummy_pre5*risky_firm_zombie
gen dummy_post_risky_zombie = dummy_post*risky_firm_zombie

gen lg_loan = log(1+total_loan_amount)

summ total_loan_given if period_new >= 28 & period_new <= 33, d
summ total_loan_given_dummy if period_new >= 28 & period_new <= 33, d
summ total_loan_amount if period_new >= 28 & period_new <= 33, d

merge m:1 creditorcode year quarter using "dta/bank_info.dta"
drop if _m==2
drop _m

merge m:1 creditorcode year using "dta/capital_adequacy_ratio.dta"
drop if _m==2
drop _m

gen log_income = log(1 + siq_ntrm_total_inc)
gen log_pat = log(1 + siq_ntrm_after_tax_profit_loss_c )

gen log_income_year = log(1 + sa_total_income)
gen log_pat_year = log(1 + sa_pat )
gen log_pbt_year = log(1 + sa_pbt)
gen log_pbit_year = log(1 + sa_pbit )
gen log_cashprofit_year = log(1 + sa_cash_profit )
gen log_gnpa_open = log(1 + sa_bd_gross_npa_opening_bal)
gen log_gnpa_close = log(1 + sa_bd_gross_npa_closing_bal)
gen log_nnpa_open = log(1 + sa_bd_net_npa_opening_bal)
gen log_nnpa_close = log(1 + sa_bd_net_npa_closing_bal)
gen log_prov_npa = log(1 + sa_bd_prov_for_npa)
gen pat_income = sa_pat/sa_total_income
gen pbt_income = sa_pbt/sa_total_income
gen pbit_income = sa_pbit/sa_total_income
gen cash_profit_income = sa_cash_profit/sa_total_income
gen gnpa_open_income = sa_bd_gross_npa_opening_bal/sa_total_income
gen gnpa_close_income = sa_bd_gross_npa_closing_bal/sa_total_income
gen nnpa_open_income = sa_bd_net_npa_opening_bal/sa_total_income
gen nnpa_close_income = sa_bd_net_npa_closing_bal/sa_total_income
gen prov_npa_income = sa_bd_prov_for_npa/sa_total_income
gen gnpa_open_loan_adv = sa_bd_gross_npa_opening_bal/sa_total_loans_and_advances
gen gnpa_close_loan_adv = sa_bd_gross_npa_closing_bal/sa_total_loans_and_advances
gen nnpa_open_loan_adv = sa_bd_net_npa_opening_bal/sa_total_loans_and_advances
gen nnpa_close_loan_adv = sa_bd_net_npa_closing_bal/sa_total_loans_and_advances
gen prov_npa_loan_adv = sa_bd_prov_for_npa/sa_total_loans_and_advances
gen gnpa_open_dep_adv = sa_bd_gross_npa_opening_bal/sa_deposits_n_advances
gen gnpa_close_dep_adv = sa_bd_gross_npa_closing_bal/sa_deposits_n_advances
gen nnpa_open_dep_adv = sa_bd_net_npa_opening_bal/sa_deposits_n_advances
gen nnpa_close_dep_adv = sa_bd_net_npa_closing_bal/sa_deposits_n_advances
gen prov_npa_dep_adv = sa_bd_prov_for_npa/sa_deposits_n_advances
gen gnpa_open_othloan_adv = sa_bd_gross_npa_opening_bal/sa_oth_loans_and_advances
gen gnpa_close_othloan_adv = sa_bd_gross_npa_closing_bal/sa_oth_loans_and_advances
gen nnpa_open_othloan_adv = sa_bd_net_npa_opening_bal/sa_oth_loans_and_advances
gen nnpa_close_othloan_adv = sa_bd_net_npa_closing_bal/sa_oth_loans_and_advances
gen prov_npa_othloan_adv = sa_bd_prov_for_npa/sa_oth_loans_and_advances

bysort creditorcode sa_finance1_cocode: egen min_period_new = min(period_new) if total_loan_given != 0
gen diff_min_period_new = period_new - min_period_new
replace diff_min_period_new = 0 if diff_min_period_new == . | diff_min_period_new < 0

//save data for main regression (public sector banks)
save "dta/tables.dta", replace

use "dta/tables.dta", replace

keep if period_new >= 28 & period_new <= 33

drop int_treat1_risky1

replace treat1 = 0 if creditorcode == 236337

gen int_treat1_risky1 = treat1*risky_firm1

gen crisis = 1

save "dta/tables_trimmed.dta", replace

use "dta/tables.dta", replace

keep if period_new >= 28 & period_new <= 33

distinct sa_finance1_cocode if total_loan_given != 0

summ total_loan_given
display r(sum)

bysort creditorcode period_new: egen sum_loan_amount = sum(total_loan_amount)
bysort creditorcode period_new risky_firm1: egen sum_loan_amount_risky = sum(total_loan_amount)

gen prop_loan_amount_risky = sum_loan_amount_risky/sum_loan_amount
replace prop_loan_amount_risky = (1 - prop_loan_amount_risky) if risky_firm1 == 0

duplicates drop creditorcode period_new, force

ttest prop_loan_amount_risky, by(treat1)



************************************************************ Pre - Period Data Making (Begin) **********************************************************

****************************************************************************************************************************************************
/* Making CEO change data into quaterly data */
****************************************************************************************************************************************************

use "raw\CEO_Tenure.dta", clear

reshape long CEO_, i(creditorCode) j(observation)
rename (creditorCode observation CEO_ sa_company_name) (creditorcode obs ceo psbname)

keep if ceo >= td(01jan2005) & ceo <= td(31dec2007)

bysort psbname: egen min_ceo = min(ceo)

format min_ceo %td

duplicates drop creditorcode, force

gen period_ceo = .
replace period_ceo = 1 if min_ceo >= td(01apr2001) & min_ceo <= td(30jun2001)
replace period_ceo = 2 if min_ceo >= td(01jul2001) & min_ceo <= td(30sep2001)
replace period_ceo = 3 if min_ceo >= td(01oct2001) & min_ceo <= td(31dec2001)
replace period_ceo = 4 if min_ceo >= td(01jan2002) & min_ceo <= td(31mar2002)
replace period_ceo = 5 if min_ceo >= td(01apr2002) & min_ceo <= td(30jun2002)
replace period_ceo = 6 if min_ceo >= td(01jul2002) & min_ceo <= td(30sep2002)
replace period_ceo = 7 if min_ceo >= td(01oct2002) & min_ceo <= td(31dec2002)
replace period_ceo = 8 if min_ceo >= td(01jan2003) & min_ceo <= td(31mar2003)
replace period_ceo = 9 if min_ceo >= td(01apr2003) & min_ceo <= td(30jun2003)
replace period_ceo = 10 if min_ceo >= td(01jul2003) & min_ceo <= td(30sep2003)
replace period_ceo = 11 if min_ceo >= td(01oct2003) & min_ceo <= td(31dec2003)
replace period_ceo = 12 if min_ceo >= td(01jan2004) & min_ceo <= td(31mar2004)
replace period_ceo = 13 if min_ceo >= td(01apr2004) & min_ceo <= td(30jun2004)
replace period_ceo = 14 if min_ceo >= td(01jul2004) & min_ceo <= td(30sep2004)
replace period_ceo = 15 if min_ceo >= td(01oct2004) & min_ceo <= td(31dec2004)
replace period_ceo = 16 if min_ceo >= td(01jan2005) & min_ceo <= td(31mar2005)
replace period_ceo = 17 if min_ceo >= td(01apr2005) & min_ceo <= td(30jun2005)
replace period_ceo = 18 if min_ceo >= td(01jul2005) & min_ceo <= td(30sep2005)
replace period_ceo = 19 if min_ceo >= td(01oct2005) & min_ceo <= td(31dec2005)
replace period_ceo = 20 if min_ceo >= td(01jan2006) & min_ceo <= td(31mar2006)
replace period_ceo = 21 if min_ceo >= td(01apr2006) & min_ceo <= td(30jun2006)
replace period_ceo = 22 if min_ceo >= td(01jul2006) & min_ceo <= td(30sep2006)
replace period_ceo = 23 if min_ceo >= td(01oct2006) & min_ceo <= td(31dec2006)
replace period_ceo = 24 if min_ceo >= td(01jan2007) & min_ceo <= td(31mar2007)
replace period_ceo = 25 if min_ceo >= td(01apr2007) & min_ceo <= td(30jun2007)
replace period_ceo = 26 if min_ceo >= td(01jul2007) & min_ceo <= td(30sep2007)
replace period_ceo = 27 if min_ceo >= td(01oct2007) & min_ceo <= td(31dec2007)
replace period_ceo = 28 if min_ceo >= td(01jan2008) & min_ceo <= td(31mar2008)
replace period_ceo = 29 if min_ceo >= td(01apr2008) & min_ceo <= td(30jun2008)
replace period_ceo = 30 if min_ceo >= td(01jul2008) & min_ceo <= td(30sep2008)
replace period_ceo = 31 if min_ceo >= td(01oct2008) & min_ceo <= td(31dec2008)
replace period_ceo = 32 if min_ceo >= td(01jan2009) & min_ceo <= td(31mar2009)
replace period_ceo = 33 if min_ceo >= td(01apr2009) & min_ceo <= td(30jun2009)
replace period_ceo = 34 if min_ceo >= td(01jul2009) & min_ceo <= td(30sep2009)
replace period_ceo = 35 if min_ceo >= td(01oct2009) & min_ceo <= td(31dec2009)
replace period_ceo = 36 if min_ceo >= td(01jan2010) & min_ceo <= td(31mar2010)

save "dta/ceo_change_quarterly_pre_crisis.dta", replace

****************************************************************************************************************************************************
/* Keeping firm-bank pairs for which loans were sanctioned between 2001 and 2010 as our focus is around this period */
/* Different dataset according to type of bank/financial companies */
****************************************************************************************************************************************************

use "dta/MCA_data_with_dates.dta", replace

keep if sanctionDate >= td(01apr1999) & sanctionDate <= td(31mar2010)

duplicates drop creditorCode firmCode, force

rename (creditorCode firmCode) (creditorcode firmcode)

merge m:1 creditorcode using "dta/public_sector_banks1.dta"

keep if _m==3
drop _m

keep creditorName creditorcode firmcode firmName psb_name

save "dta/MCA_data_psb_pre_crisis.dta", replace

****************************************************************************************************************************************************
/* Creating balanced panel from firm-bank pair (quarter)*/
****************************************************************************************************************************************************

use "dta/MCA_data_psb_pre_crisis.dta", replace
append using "dta/MCA_data_psb_pre_crisis.dta"
append using "dta/MCA_data_psb_pre_crisis.dta"
append using "dta/MCA_data_psb_pre_crisis.dta"

bysort creditorcode firmcode: gen quarter = _n

save "dta/MCA_data_psb_only_quarter_pre_crisis.dta", replace

****************************************************************************************************************************************************
/* Creating balanced panel from firm-bank pair (quarter-year)*/
****************************************************************************************************************************************************

use "dta/MCA_data_psb_only_quarter_pre_crisis.dta", replace
append using "dta/MCA_data_psb_only_quarter_pre_crisis.dta"
append using "dta/MCA_data_psb_only_quarter_pre_crisis.dta"
append using "dta/MCA_data_psb_only_quarter_pre_crisis.dta"
append using "dta/MCA_data_psb_only_quarter_pre_crisis.dta"
append using "dta/MCA_data_psb_only_quarter_pre_crisis.dta"
append using "dta/MCA_data_psb_only_quarter_pre_crisis.dta"
append using "dta/MCA_data_psb_only_quarter_pre_crisis.dta"
append using "dta/MCA_data_psb_only_quarter_pre_crisis.dta"
append using "dta/MCA_data_psb_only_quarter_pre_crisis.dta"
append using "dta/MCA_data_psb_only_quarter_pre_crisis.dta"

bysort creditorcode firmcode quarter: gen year = _n

replace year = year + 1999

save "dta/MCA_data_psb_year_quarter_blank_pre_crisis.dta", replace

****************************************************************************************************************************************************
/* Giving quarter-year values to the original data */
****************************************************************************************************************************************************

use "dta/MCA_data_with_dates.dta", replace

keep if sanctionDate >= td(01apr1999) & sanctionDate <= td(31mar2010)

rename (creditorCode firmCode) (creditorcode firmcode)

merge m:1 creditorcode using "dta/public_sector_banks1.dta"

keep if _m==3
drop _m

gen period = .

replace period = -7 if sanctionDate >= td(01apr1999) & sanctionDate <= td(30jun1999)
replace period = -6 if sanctionDate >= td(01jul1999) & sanctionDate <= td(30sep1999)
replace period = -5 if sanctionDate >= td(01oct1999) & sanctionDate <= td(31dec1999)
replace period = -4 if sanctionDate >= td(01jan2000) & sanctionDate <= td(31mar2000)
replace period = -3 if sanctionDate >= td(01apr2000) & sanctionDate <= td(30jun2000)
replace period = -2 if sanctionDate >= td(01jul2000) & sanctionDate <= td(30sep2000)
replace period = -1 if sanctionDate >= td(01oct2000) & sanctionDate <= td(31dec2000)
replace period = 0 if sanctionDate >= td(01jan2001) & sanctionDate <= td(31mar2001)
replace period = 1 if sanctionDate >= td(01apr2001) & sanctionDate <= td(30jun2001)
replace period = 2 if sanctionDate >= td(01jul2001) & sanctionDate <= td(30sep2001)
replace period = 3 if sanctionDate >= td(01oct2001) & sanctionDate <= td(31dec2001)
replace period = 4 if sanctionDate >= td(01jan2002) & sanctionDate <= td(31mar2002)
replace period = 5 if sanctionDate >= td(01apr2002) & sanctionDate <= td(30jun2002)
replace period = 6 if sanctionDate >= td(01jul2002) & sanctionDate <= td(30sep2002)
replace period = 7 if sanctionDate >= td(01oct2002) & sanctionDate <= td(31dec2002)
replace period = 8 if sanctionDate >= td(01jan2003) & sanctionDate <= td(31mar2003)
replace period = 9 if sanctionDate >= td(01apr2003) & sanctionDate <= td(30jun2003)
replace period = 10 if sanctionDate >= td(01jul2003) & sanctionDate <= td(30sep2003)
replace period = 11 if sanctionDate >= td(01oct2003) & sanctionDate <= td(31dec2003)
replace period = 12 if sanctionDate >= td(01jan2004) & sanctionDate <= td(31mar2004)
replace period = 13 if sanctionDate >= td(01apr2004) & sanctionDate <= td(30jun2004)
replace period = 14 if sanctionDate >= td(01jul2004) & sanctionDate <= td(30sep2004)
replace period = 15 if sanctionDate >= td(01oct2004) & sanctionDate <= td(31dec2004)
replace period = 16 if sanctionDate >= td(01jan2005) & sanctionDate <= td(31mar2005)
replace period = 17 if sanctionDate >= td(01apr2005) & sanctionDate <= td(30jun2005)
replace period = 18 if sanctionDate >= td(01jul2005) & sanctionDate <= td(30sep2005)
replace period = 19 if sanctionDate >= td(01oct2005) & sanctionDate <= td(31dec2005)
replace period = 20 if sanctionDate >= td(01jan2006) & sanctionDate <= td(31mar2006)
replace period = 21 if sanctionDate >= td(01apr2006) & sanctionDate <= td(30jun2006)
replace period = 22 if sanctionDate >= td(01jul2006) & sanctionDate <= td(30sep2006)
replace period = 23 if sanctionDate >= td(01oct2006) & sanctionDate <= td(31dec2006)
replace period = 24 if sanctionDate >= td(01jan2007) & sanctionDate <= td(31mar2007)
replace period = 25 if sanctionDate >= td(01apr2007) & sanctionDate <= td(30jun2007)
replace period = 26 if sanctionDate >= td(01jul2007) & sanctionDate <= td(30sep2007)
replace period = 27 if sanctionDate >= td(01oct2007) & sanctionDate <= td(31dec2007)
replace period = 28 if sanctionDate >= td(01jan2008) & sanctionDate <= td(31mar2008)
replace period = 29 if sanctionDate >= td(01apr2008) & sanctionDate <= td(30jun2008)
replace period = 30 if sanctionDate >= td(01jul2008) & sanctionDate <= td(30sep2008)
replace period = 31 if sanctionDate >= td(01oct2008) & sanctionDate <= td(31dec2008)
replace period = 32 if sanctionDate >= td(01jan2009) & sanctionDate <= td(31mar2009)
replace period = 33 if sanctionDate >= td(01apr2009) & sanctionDate <= td(30jun2009)
replace period = 34 if sanctionDate >= td(01jul2009) & sanctionDate <= td(30sep2009)
replace period = 35 if sanctionDate >= td(01oct2009) & sanctionDate <= td(31dec2009)
replace period = 36 if sanctionDate >= td(01jan2010) & sanctionDate <= td(31mar2010)

gen period_repayment = .

replace period_repayment = -7 if repaymentDate >= td(01apr1999) & repaymentDate <= td(30jun1999)
replace period_repayment = -6 if repaymentDate >= td(01jul1999) & repaymentDate <= td(30sep1999)
replace period_repayment = -5 if repaymentDate >= td(01oct1999) & repaymentDate <= td(31dec1999)
replace period_repayment = -4 if repaymentDate >= td(01jan2000) & repaymentDate <= td(31mar2000)
replace period_repayment = -3 if repaymentDate >= td(01apr2000) & repaymentDate <= td(30jun2000)
replace period_repayment = -2 if repaymentDate >= td(01jul2000) & repaymentDate <= td(30sep2000)
replace period_repayment = -1 if repaymentDate >= td(01oct2000) & repaymentDate <= td(31dec2000)
replace period_repayment = 0 if repaymentDate >= td(01jan2001) & repaymentDate <= td(31mar2001)
replace period_repayment = 1 if repaymentDate >= td(01apr2001) & repaymentDate <= td(30jun2001)
replace period_repayment = 2 if repaymentDate >= td(01jul2001) & repaymentDate <= td(30sep2001)
replace period_repayment = 3 if repaymentDate >= td(01oct2001) & repaymentDate <= td(31dec2001)
replace period_repayment = 4 if repaymentDate >= td(01jan2002) & repaymentDate <= td(31mar2002)
replace period_repayment = 5 if repaymentDate >= td(01apr2002) & repaymentDate <= td(30jun2002)
replace period_repayment = 6 if repaymentDate >= td(01jul2002) & repaymentDate <= td(30sep2002)
replace period_repayment = 7 if repaymentDate >= td(01oct2002) & repaymentDate <= td(31dec2002)
replace period_repayment = 8 if repaymentDate >= td(01jan2003) & repaymentDate <= td(31mar2003)
replace period_repayment = 9 if repaymentDate >= td(01apr2003) & repaymentDate <= td(30jun2003)
replace period_repayment = 10 if repaymentDate >= td(01jul2003) & repaymentDate <= td(30sep2003)
replace period_repayment = 11 if repaymentDate >= td(01oct2003) & repaymentDate <= td(31dec2003)
replace period_repayment = 12 if repaymentDate >= td(01jan2004) & repaymentDate <= td(31mar2004)
replace period_repayment = 13 if repaymentDate >= td(01apr2004) & repaymentDate <= td(30jun2004)
replace period_repayment = 14 if repaymentDate >= td(01jul2004) & repaymentDate <= td(30sep2004)
replace period_repayment = 15 if repaymentDate >= td(01oct2004) & repaymentDate <= td(31dec2004)
replace period_repayment = 16 if repaymentDate >= td(01jan2005) & repaymentDate <= td(31mar2005)
replace period_repayment = 17 if repaymentDate >= td(01apr2005) & repaymentDate <= td(30jun2005)
replace period_repayment = 18 if repaymentDate >= td(01jul2005) & repaymentDate <= td(30sep2005)
replace period_repayment = 19 if repaymentDate >= td(01oct2005) & repaymentDate <= td(31dec2005)
replace period_repayment = 20 if repaymentDate >= td(01jan2006) & repaymentDate <= td(31mar2006)
replace period_repayment = 21 if repaymentDate >= td(01apr2006) & repaymentDate <= td(30jun2006)
replace period_repayment = 22 if repaymentDate >= td(01jul2006) & repaymentDate <= td(30sep2006)
replace period_repayment = 23 if repaymentDate >= td(01oct2006) & repaymentDate <= td(31dec2006)
replace period_repayment = 24 if repaymentDate >= td(01jan2007) & repaymentDate <= td(31mar2007)
replace period_repayment = 25 if repaymentDate >= td(01apr2007) & repaymentDate <= td(30jun2007)
replace period_repayment = 26 if repaymentDate >= td(01jul2007) & repaymentDate <= td(30sep2007)
replace period_repayment = 27 if repaymentDate >= td(01oct2007) & repaymentDate <= td(31dec2007)
replace period_repayment = 28 if repaymentDate >= td(01jan2008) & repaymentDate <= td(31mar2008)
replace period_repayment = 29 if repaymentDate >= td(01apr2008) & repaymentDate <= td(30jun2008)
replace period_repayment = 30 if repaymentDate >= td(01jul2008) & repaymentDate <= td(30sep2008)
replace period_repayment = 31 if repaymentDate >= td(01oct2008) & repaymentDate <= td(31dec2008)
replace period_repayment = 32 if repaymentDate >= td(01jan2009) & repaymentDate <= td(31mar2009)
replace period_repayment = 33 if repaymentDate >= td(01apr2009) & repaymentDate <= td(30jun2009)
replace period_repayment = 34 if repaymentDate >= td(01jul2009) & repaymentDate <= td(30sep2009)
replace period_repayment = 35 if repaymentDate >= td(01oct2009) & repaymentDate <= td(31dec2009)
replace period_repayment = 36 if repaymentDate >= td(01jan2010) & repaymentDate <= td(31mar2010)

gen year = int((period-1)/4)
replace year = year + 2002
gen quarter = mod(period,4)
replace quarter = 4 if quarter == 0

replace year = 2001 if period >= -3 & period <= 0
replace quarter = 1 if period == -3
replace quarter = 2 if period == -2
replace quarter = 3 if period == -1
replace quarter = 4 if period == 0

replace year = 2000 if period >= -7 & period <= -4
replace quarter = 1 if period == -7
replace quarter = 2 if period == -6
replace quarter = 3 if period == -5
replace quarter = 4 if period == -4

gen zero_loan_amount = 0
replace zero_loan_amount = 1 if loanAmount == 0

bysort creditorcode firmcode year quarter: egen total_loan_amount = sum(loanAmount)
bysort creditorcode firmcode year quarter: egen total_loan_given = sum(psb)

duplicates drop creditorcode firmcode year quarter, force

save "dta/MCA_data_psb_quarter_included_pre_crisis.dta", replace

****************************************************************************************************************************************************
/* Merging the originial data with balanced panel data since we have quarter-year values in original data */
****************************************************************************************************************************************************

use "dta/MCA_data_psb_year_quarter_blank_pre_crisis.dta", replace
merge 1:1 creditorcode firmcode year quarter using "dta/MCA_data_psb_quarter_included_pre_crisis.dta"

bysort creditorcode firmcode: egen first_sanction_period = min(period)
bysort creditorcode firmcode: egen last_repayment_period = max(period_repayment)

replace total_loan_amount = 0 if _m==1
replace total_loan_given = 0 if _m==1

drop _m

gen period_new = 4*(year-2002) + quarter

rename firmcode sa_finance1_cocode

save "dta/MCA_data_psb_year_quarter_final_pre_crisis.dta", replace

****************************************************************************************************************************************************
/* Getting firm-bank relationships that existed before the crisis (period chosen on our discretion)*/
****************************************************************************************************************************************************

use "dta/MCA_data_psb_year_quarter_final_pre_crisis.dta", replace

keep if period_new >= -3 & period_new <= 12
keep if total_loan_given != 0
duplicates drop sa_finance1_cocode creditorcode, force

keep sa_finance1_cocode creditorcode 

distinct sa_finance1_cocode
distinct creditorcode

save "dta/prev_yrs_loan_firms_quarterly_pre_crisis.dta", replace

****************************************************************************************************************************************************
/* Creating restructuring data (modifications) */
****************************************************************************************************************************************************

use "raw\LoansInMCA(2).dta", replace

keep if sanctionDate  >= td(01apr2000) & sanctionDate  <= td(31dec2004)
keep if modificationDate  >= td(01apr2000) & modificationDate  <= td(31mar2010)

rename (creditorCode firmCode) (creditorcode firmcode)

merge m:1 creditorcode using "dta/public_sector_banks1.dta"

keep if _m==3
drop _m

gen period = .

replace period = -7 if modificationDate >= td(01apr1999) & modificationDate <= td(30jun1999)
replace period = -6 if modificationDate >= td(01jul1999) & modificationDate <= td(30sep1999)
replace period = -5 if modificationDate >= td(01oct1999) & modificationDate <= td(31dec1999)
replace period = -4 if modificationDate >= td(01jan2000) & modificationDate <= td(31mar2000)
replace period = -3 if modificationDate >= td(01apr2000) & modificationDate <= td(30jun2000)
replace period = -2 if modificationDate >= td(01jul2000) & modificationDate <= td(30sep2000)
replace period = -1 if modificationDate >= td(01oct2000) & modificationDate <= td(31dec2000)
replace period = 0 if modificationDate >= td(01jan2001) & modificationDate <= td(31mar2001)
replace period = 1 if modificationDate >= td(01apr2001) & modificationDate <= td(30jun2001)
replace period = 2 if modificationDate >= td(01jul2001) & modificationDate <= td(30sep2001)
replace period = 3 if modificationDate >= td(01oct2001) & modificationDate <= td(31dec2001)
replace period = 4 if modificationDate >= td(01jan2002) & modificationDate <= td(31mar2002)
replace period = 5 if modificationDate >= td(01apr2002) & modificationDate <= td(30jun2002)
replace period = 6 if modificationDate >= td(01jul2002) & modificationDate <= td(30sep2002)
replace period = 7 if modificationDate >= td(01oct2002) & modificationDate <= td(31dec2002)
replace period = 8 if modificationDate >= td(01jan2003) & modificationDate <= td(31mar2003)
replace period = 9 if modificationDate >= td(01apr2003) & modificationDate <= td(30jun2003)
replace period = 10 if modificationDate >= td(01jul2003) & modificationDate <= td(30sep2003)
replace period = 11 if modificationDate >= td(01oct2003) & modificationDate <= td(31dec2003)
replace period = 12 if modificationDate >= td(01jan2004) & modificationDate <= td(31mar2004)
replace period = 13 if modificationDate >= td(01apr2004) & modificationDate <= td(30jun2004)
replace period = 14 if modificationDate >= td(01jul2004) & modificationDate <= td(30sep2004)
replace period = 15 if modificationDate >= td(01oct2004) & modificationDate <= td(31dec2004)
replace period = 16 if modificationDate >= td(01jan2005) & modificationDate <= td(31mar2005)
replace period = 17 if modificationDate >= td(01apr2005) & modificationDate <= td(30jun2005)
replace period = 18 if modificationDate >= td(01jul2005) & modificationDate <= td(30sep2005)
replace period = 19 if modificationDate >= td(01oct2005) & modificationDate <= td(31dec2005)
replace period = 20 if modificationDate >= td(01jan2006) & modificationDate <= td(31mar2006)
replace period = 21 if modificationDate >= td(01apr2006) & modificationDate <= td(30jun2006)
replace period = 22 if modificationDate >= td(01jul2006) & modificationDate <= td(30sep2006)
replace period = 23 if modificationDate >= td(01oct2006) & modificationDate <= td(31dec2006)
replace period = 24 if modificationDate >= td(01jan2007) & modificationDate <= td(31mar2007)
replace period = 25 if modificationDate >= td(01apr2007) & modificationDate <= td(30jun2007)
replace period = 26 if modificationDate >= td(01jul2007) & modificationDate <= td(30sep2007)
replace period = 27 if modificationDate >= td(01oct2007) & modificationDate <= td(31dec2007)
replace period = 28 if modificationDate >= td(01jan2008) & modificationDate <= td(31mar2008)
replace period = 29 if modificationDate >= td(01apr2008) & modificationDate <= td(30jun2008)
replace period = 30 if modificationDate >= td(01jul2008) & modificationDate <= td(30sep2008)
replace period = 31 if modificationDate >= td(01oct2008) & modificationDate <= td(31dec2008)
replace period = 32 if modificationDate >= td(01jan2009) & modificationDate <= td(31mar2009)
replace period = 33 if modificationDate >= td(01apr2009) & modificationDate <= td(30jun2009)
replace period = 34 if modificationDate >= td(01jul2009) & modificationDate <= td(30sep2009)
replace period = 35 if modificationDate >= td(01oct2009) & modificationDate <= td(31dec2009)
replace period = 36 if modificationDate >= td(01jan2010) & modificationDate <= td(31mar2010)

gen period_sanction = .

replace period_sanction = -7 if sanctionDate >= td(01apr1999) & sanctionDate <= td(30jun1999)
replace period_sanction = -6 if sanctionDate >= td(01jul1999) & sanctionDate <= td(30sep1999)
replace period_sanction = -5 if sanctionDate >= td(01oct1999) & sanctionDate <= td(31dec1999)
replace period_sanction = -4 if sanctionDate >= td(01jan2000) & sanctionDate <= td(31mar2000)
replace period_sanction = -3 if sanctionDate >= td(01apr2000) & sanctionDate <= td(30jun2000)
replace period_sanction = -2 if sanctionDate >= td(01jul2000) & sanctionDate <= td(30sep2000)
replace period_sanction = -1 if sanctionDate >= td(01oct2000) & sanctionDate <= td(31dec2000)
replace period_sanction = 0 if sanctionDate >= td(01jan2001) & sanctionDate <= td(31mar2001)
replace period_sanction = 1 if sanctionDate >= td(01apr2001) & sanctionDate <= td(30jun2001)
replace period_sanction = 2 if sanctionDate >= td(01jul2001) & sanctionDate <= td(30sep2001)
replace period_sanction = 3 if sanctionDate >= td(01oct2001) & sanctionDate <= td(31dec2001)
replace period_sanction = 4 if sanctionDate >= td(01jan2002) & sanctionDate <= td(31mar2002)
replace period_sanction = 5 if sanctionDate >= td(01apr2002) & sanctionDate <= td(30jun2002)
replace period_sanction = 6 if sanctionDate >= td(01jul2002) & sanctionDate <= td(30sep2002)
replace period_sanction = 7 if sanctionDate >= td(01oct2002) & sanctionDate <= td(31dec2002)
replace period_sanction = 8 if sanctionDate >= td(01jan2003) & sanctionDate <= td(31mar2003)
replace period_sanction = 9 if sanctionDate >= td(01apr2003) & sanctionDate <= td(30jun2003)
replace period_sanction = 10 if sanctionDate >= td(01jul2003) & sanctionDate <= td(30sep2003)
replace period_sanction = 11 if sanctionDate >= td(01oct2003) & sanctionDate <= td(31dec2003)
replace period_sanction = 12 if sanctionDate >= td(01jan2004) & sanctionDate <= td(31mar2004)
replace period_sanction = 13 if sanctionDate >= td(01apr2004) & sanctionDate <= td(30jun2004)
replace period_sanction = 14 if sanctionDate >= td(01jul2004) & sanctionDate <= td(30sep2004)
replace period_sanction = 15 if sanctionDate >= td(01oct2004) & sanctionDate <= td(31dec2004)
replace period_sanction = 16 if sanctionDate >= td(01jan2005) & sanctionDate <= td(31mar2005)
replace period_sanction = 17 if sanctionDate >= td(01apr2005) & sanctionDate <= td(30jun2005)
replace period_sanction = 18 if sanctionDate >= td(01jul2005) & sanctionDate <= td(30sep2005)
replace period_sanction = 19 if sanctionDate >= td(01oct2005) & sanctionDate <= td(31dec2005)
replace period_sanction = 20 if sanctionDate >= td(01jan2006) & sanctionDate <= td(31mar2006)
replace period_sanction = 21 if sanctionDate >= td(01apr2006) & sanctionDate <= td(30jun2006)
replace period_sanction = 22 if sanctionDate >= td(01jul2006) & sanctionDate <= td(30sep2006)
replace period_sanction = 23 if sanctionDate >= td(01oct2006) & sanctionDate <= td(31dec2006)
replace period_sanction = 24 if sanctionDate >= td(01jan2007) & sanctionDate <= td(31mar2007)
replace period_sanction = 25 if sanctionDate >= td(01apr2007) & sanctionDate <= td(30jun2007)
replace period_sanction = 26 if sanctionDate >= td(01jul2007) & sanctionDate <= td(30sep2007)
replace period_sanction = 27 if sanctionDate >= td(01oct2007) & sanctionDate <= td(31dec2007)
replace period_sanction = 28 if sanctionDate >= td(01jan2008) & sanctionDate <= td(31mar2008)
replace period_sanction = 29 if sanctionDate >= td(01apr2008) & sanctionDate <= td(30jun2008)
replace period_sanction = 30 if sanctionDate >= td(01jul2008) & sanctionDate <= td(30sep2008)
replace period_sanction = 31 if sanctionDate >= td(01oct2008) & sanctionDate <= td(31dec2008)
replace period_sanction = 32 if sanctionDate >= td(01jan2009) & sanctionDate <= td(31mar2009)
replace period_sanction = 33 if sanctionDate >= td(01apr2009) & sanctionDate <= td(30jun2009)
replace period_sanction = 34 if sanctionDate >= td(01jul2009) & sanctionDate <= td(30sep2009)
replace period_sanction = 35 if sanctionDate >= td(01oct2009) & sanctionDate <= td(31dec2009)
replace period_sanction = 36 if sanctionDate >= td(01jan2010) & sanctionDate <= td(31mar2010)

gen year = int((period-1)/4)
replace year = year + 2002
gen quarter = mod(period,4)
replace quarter = 4 if quarter == 0

replace year = 2001 if period >= -3 & period <= 0
replace quarter = 1 if period == -3
replace quarter = 2 if period == -2
replace quarter = 3 if period == -1
replace quarter = 4 if period == 0

bysort creditorcode firmcode year quarter: gen num_of_modifications = _N

gen same_period_modification = 0
replace same_period_modification = 1 if period == period_sanction

bysort creditorcode firmcode year quarter: egen num_same_modifications = sum(same_period_modification)

gen num_of_modifications2 = num_of_modifications - num_same_modifications

duplicates drop creditorcode firmcode year quarter, force

rename firmcode sa_finance1_cocode

keep creditorcode sa_finance1_cocode year quarter period num_of_modifications num_of_modifications2 loanAmount

save "dta/restructuring_psb_final_pre_crisis.dta", replace

****************************************************************************************************************************************************
/* Identifying risky (low quality) firms in pre-period */
****************************************************************************************************************************************************

import delimited "raw\firms_interest_coverage.txt", delimiter("|") clear

destring sa_total_income-sa_no_of_employees, force replace

tostring sa_finance1_year, force replace
gen day = substr(sa_finance1_year,7,2)
gen month = substr(sa_finance1_year,5,2)
gen year = substr(sa_finance1_year,1,4)

destring day-year, force replace

duplicates tag sa_finance1_cocode year, gen(x)
bysort sa_finance1_cocode: gen march = 1 if x>0 & month == 3
bysort sa_finance1_cocode: egen max_march = max(march) if x>0
drop if x>0 & max_march == 1 & march != 1
sort sa_finance1_cocode year month
bysort sa_finance1_cocode year: gen dup_n = _n if x > 0 & max_march != 1 
drop if dup_n > 1 & dup_n != .
sort sa_finance1_cocode year month day
bysort sa_finance1_cocode year: egen march_dup = sum(march) if x > 0 & max_march == 1 
bysort sa_finance1_cocode year: gen march_dup_n = _n if march_dup > 1 & march_dup != .
bysort sa_finance1_cocode year: gen march_dup_total_n = _N if march_dup > 1 & march_dup != .
drop if march_dup_n < march_dup_total_n & march_dup_n != .

drop x-march_dup_total_n

merge m:1 sa_finance1_cocode using "dta/industry_code.dta"
keep if _m==3
drop _m

merge m:1 sa_finance1_cocode using "dta/age_of_firms.dta"
drop if _m==2
drop _m

gen age = year - incorporation_year

tsset sa_finance1_cocode year, yearly

gen int_cov_ratio = sa_pbit/sa_interest_exp
drop if int_cov_ratio == .

gen risky_firm_zombie = 0
replace risky_firm_zombie = 1 if (int_cov_ratio < 1) & (L1.int_cov_ratio < 1) & (L2.int_cov_ratio < 1) & (age >= 10)

keep if year == 2004

bysort sa_finance1_cocode: egen avg_int_cov_ratio = mean(int_cov_ratio)
gen risky_firm1 = 0
replace risky_firm1 = 1 if avg_int_cov_ratio < 1

gen low_int_cov_ratio = 0
replace low_int_cov_ratio = 1 if int_cov_ratio < 1

bysort sa_finance1_cocode: egen risky_firm2 = max(low_int_cov_ratio)
bysort sa_finance1_cocode: egen risky_firm3 = min(low_int_cov_ratio)

duplicates drop sa_finance1_cocode, force

keep sa_finance1_cocode co_industry_name sa_company_name risky_firm1 risky_firm2 risky_firm3 avg_int_cov_ratio risky_firm_zombie

save "dta/firms_interest_coverage_pre_crisis.dta", replace

****************************************************************************************************************************************************
/* Identifying risky (low quality) firms in crisis period */
****************************************************************************************************************************************************

import delimited "raw\firms_interest_coverage.txt", delimiter("|") clear

destring sa_total_income-sa_debt_equity, force replace

tostring sa_finance1_year, force replace
gen day = substr(sa_finance1_year,7,2)
gen month = substr(sa_finance1_year,5,2)
gen year = substr(sa_finance1_year,1,4)

destring day-year, force replace

duplicates tag sa_finance1_cocode year, gen(x)
bysort sa_finance1_cocode: gen march = 1 if x>0 & month == 3
bysort sa_finance1_cocode: egen max_march = max(march) if x>0
drop if x>0 & max_march == 1 & march != 1
sort sa_finance1_cocode year month
bysort sa_finance1_cocode year: gen dup_n = _n if x > 0 & max_march != 1 
drop if dup_n > 1 & dup_n != .
sort sa_finance1_cocode year month day
bysort sa_finance1_cocode year: egen march_dup = sum(march) if x > 0 & max_march == 1 
bysort sa_finance1_cocode year: gen march_dup_n = _n if march_dup > 1 & march_dup != .
bysort sa_finance1_cocode year: gen march_dup_total_n = _N if march_dup > 1 & march_dup != .
drop if march_dup_n < march_dup_total_n & march_dup_n != .

drop x-march_dup_total_n

merge m:1 sa_finance1_cocode using "dta/industry_code.dta"
keep if _m==3
drop _m

keep if year == 2006

gen int_cov_ratio = sa_pbit/sa_interest_exp
drop if int_cov_ratio == .
bysort sa_finance1_cocode: egen avg_int_cov_ratio = mean(int_cov_ratio)
gen risky_firm_crisis1 = 0
replace risky_firm_crisis1 = 1 if avg_int_cov_ratio < 1

gen low_int_cov_ratio = 0
replace low_int_cov_ratio = 1 if int_cov_ratio < 1

bysort sa_finance1_cocode: egen risky_firm_crisis2 = max(low_int_cov_ratio)
bysort sa_finance1_cocode: egen risky_firm_crisis3 = min(low_int_cov_ratio)

duplicates drop sa_finance1_cocode, force

keep sa_finance1_cocode co_industry_name sa_company_name risky_firm_crisis1 risky_firm_crisis2 risky_firm_crisis3

save "dta/firms_interest_coverage_crisis_pre_crisis.dta", replace

****************************************************************************************************************************************************
/* Keeping ony those firm-bank pairs that had existing relationship in the merged data (Loan Dummy data) */
****************************************************************************************************************************************************

use "dta/MCA_data_psb_year_quarter_final_pre_crisis.dta", replace

merge m:1 sa_finance1_cocode creditorcode using "dta/prev_yrs_loan_firms_quarterly_pre_crisis.dta"

keep if _m==3
drop _m

merge m:1 creditorcode using "dta/ceo_change_quarterly_pre_crisis.dta"

replace period_ceo = 10000 if _m==1

drop if _m==2 
drop _m

gen treat1 = 0
replace treat1 = 1 if period_new >= period_ceo

gen treat2 = 0
replace treat2 = 1 if period_new > period_ceo

bysort creditorcode: egen sum_treat1 = sum(treat1)

count if period_new >= 28 & period_new <= 33

merge m:1 sa_finance1_cocode using "dta/firms_interest_coverage_pre_crisis.dta"
keep if _m==3
drop _m

distinct sa_finance1_cocode if period_new >= 28 & period_new <= 33 & risky_firm1 == 1
distinct sa_finance1_cocode if period_new >= 28 & period_new <= 33 & risky_firm1 == 0

count if period_new >= 28 & period_new <= 33

merge m:1 sa_finance1_cocode using "dta/industry.dta"
drop if _m==2
drop _m

gen int_treat1_risky1 = treat1*risky_firm1
gen int_treat1_risky_zombie = treat1*risky_firm_zombie
gen int_treat1_risky2 = treat1*risky_firm2
gen int_treat1_risky3 = treat1*risky_firm3
gen int_treat2_risky1 = treat2*risky_firm1
gen int_treat2_risky2 = treat2*risky_firm2
gen int_treat2_risky3 = treat2*risky_firm3

gen total_loan_given_dummy = 0
replace total_loan_given_dummy = 1 if total_loan_given > 0

egen firm_year = group(year sa_finance1_cocode)
egen firm_period = group(period_new  sa_finance1_cocode)
gen nic_prod_code_2 = substr(nic_prod_code,1,2)
gen nic_prod_code_3 = substr(nic_prod_code,1,3)

bysort creditorcode: egen first_period_ceo = min(period_new) if treat1 == 1
bysort creditorcode: egen min_first_period_ceo = min(first_period_ceo)

gen dist_from_ceo_change = period_new - min_first_period

gen dummy_pre1 = 0
replace dummy_pre1 = 1 if dist_from_ceo_change == -1
gen dummy_pre2 = 0
replace dummy_pre2 = 1 if dist_from_ceo_change == -2
gen dummy_pre3 = 0
replace dummy_pre3 = 1 if dist_from_ceo_change == -3
gen dummy_pre4 = 0
replace dummy_pre4 = 1 if dist_from_ceo_change == -4
gen dummy_pre5 = 0
replace dummy_pre5 = 1 if dist_from_ceo_change == -5
gen dummy_post = 0
replace dummy_post = 1 if dist_from_ceo_change >= 0 & dist_from_ceo_change != .

gen dummy_pre1_risky1 = dummy_pre1*risky_firm1
gen dummy_pre2_risky1 = dummy_pre2*risky_firm1
gen dummy_pre3_risky1 = dummy_pre3*risky_firm1
gen dummy_pre4_risky1 = dummy_pre4*risky_firm1
gen dummy_pre5_risky1 = dummy_pre5*risky_firm1
gen dummy_post_risky1 = dummy_post*risky_firm1

gen dummy_pre1_risky_zombie = dummy_pre1*risky_firm_zombie
gen dummy_pre2_risky_zombie = dummy_pre2*risky_firm_zombie
gen dummy_pre3_risky_zombie = dummy_pre3*risky_firm_zombie
gen dummy_pre4_risky_zombie = dummy_pre4*risky_firm_zombie
gen dummy_pre5_risky_zombie = dummy_pre5*risky_firm_zombie
gen dummy_post_risky_zombie = dummy_post*risky_firm_zombie

gen lg_loan = log(1+total_loan_amount)

summ total_loan_given if period_new >= 28 & period_new <= 33, d
summ total_loan_given_dummy if period_new >= 28 & period_new <= 33, d
summ total_loan_amount if period_new >= 28 & period_new <= 33, d

merge m:1 creditorcode year quarter using "dta/bank_info.dta"
drop if _m==2
drop _m

merge m:1 creditorcode year using "dta/capital_adequacy_ratio.dta"
drop if _m==2
drop _m

gen log_income = log(1 + siq_ntrm_total_inc)
gen log_pat = log(1 + siq_ntrm_after_tax_profit_loss_c )

gen log_income_year = log(1 + sa_total_income)
gen log_pat_year = log(1 + sa_pat )
gen log_pbt_year = log(1 + sa_pbt)
gen log_pbit_year = log(1 + sa_pbit )
gen log_cashprofit_year = log(1 + sa_cash_profit )
gen log_gnpa_open = log(1 + sa_bd_gross_npa_opening_bal)
gen log_gnpa_close = log(1 + sa_bd_gross_npa_closing_bal)
gen log_nnpa_open = log(1 + sa_bd_net_npa_opening_bal)
gen log_nnpa_close = log(1 + sa_bd_net_npa_closing_bal)
gen log_prov_npa = log(1 + sa_bd_prov_for_npa)
gen pat_income = sa_pat/sa_total_income
gen pbt_income = sa_pbt/sa_total_income
gen pbit_income = sa_pbit/sa_total_income
gen cash_profit_income = sa_cash_profit/sa_total_income
gen gnpa_open_income = sa_bd_gross_npa_opening_bal/sa_total_income
gen gnpa_close_income = sa_bd_gross_npa_closing_bal/sa_total_income
gen nnpa_open_income = sa_bd_net_npa_opening_bal/sa_total_income
gen nnpa_close_income = sa_bd_net_npa_closing_bal/sa_total_income
gen prov_npa_income = sa_bd_prov_for_npa/sa_total_income
gen gnpa_open_loan_adv = sa_bd_gross_npa_opening_bal/sa_total_loans_and_advances
gen gnpa_close_loan_adv = sa_bd_gross_npa_closing_bal/sa_total_loans_and_advances
gen nnpa_open_loan_adv = sa_bd_net_npa_opening_bal/sa_total_loans_and_advances
gen nnpa_close_loan_adv = sa_bd_net_npa_closing_bal/sa_total_loans_and_advances
gen prov_npa_loan_adv = sa_bd_prov_for_npa/sa_total_loans_and_advances
gen gnpa_open_dep_adv = sa_bd_gross_npa_opening_bal/sa_deposits_n_advances
gen gnpa_close_dep_adv = sa_bd_gross_npa_closing_bal/sa_deposits_n_advances
gen nnpa_open_dep_adv = sa_bd_net_npa_opening_bal/sa_deposits_n_advances
gen nnpa_close_dep_adv = sa_bd_net_npa_closing_bal/sa_deposits_n_advances
gen prov_npa_dep_adv = sa_bd_prov_for_npa/sa_deposits_n_advances
gen gnpa_open_othloan_adv = sa_bd_gross_npa_opening_bal/sa_oth_loans_and_advances
gen gnpa_close_othloan_adv = sa_bd_gross_npa_closing_bal/sa_oth_loans_and_advances
gen nnpa_open_othloan_adv = sa_bd_net_npa_opening_bal/sa_oth_loans_and_advances
gen nnpa_close_othloan_adv = sa_bd_net_npa_closing_bal/sa_oth_loans_and_advances
gen prov_npa_othloan_adv = sa_bd_prov_for_npa/sa_oth_loans_and_advances

bysort creditorcode sa_finance1_cocode: egen min_period_new = min(period_new) if total_loan_given != 0
gen diff_min_period_new = period_new - min_period_new
replace diff_min_period_new = 0 if diff_min_period_new == . | diff_min_period_new < 0

//save data for main regression (public sector banks)
save "dta/tables_pre_crisis.dta", replace

keep if period_new >= 16 & period_new <= 27

gen crisis = 0

save "dta/tables_pre_crisis_trimmed.dta", replace

****************************************************************************************************************************************************
/* Creating restructuring data and regression table */
****************************************************************************************************************************************************

use "dta/MCA_data_psb_year_quarter_final.dta", replace

merge m:1 sa_finance1_cocode creditorcode using "dta/prev_yrs_loan_firms_quarterly.dta"

keep if _m==3
drop _m

merge m:1 creditorcode using "dta/ceo_change_quarterly_new.dta"

replace period_ceo_ext = 10000 if _m==1

drop if _m==2 
drop _m

gen treat1 = 0
replace treat1 = 1 if period_new >= period_ceo_ext

gen treat2 = 0
replace treat2 = 1 if period_new > period_ceo_ext

bysort creditorcode: egen sum_treat1 = sum(treat1)

merge m:1 sa_finance1_cocode creditorcode year quarter using "dta/restructuring_psb_final.dta"

replace num_of_modifications = 0 if _m==1
replace num_of_modifications2 = 0 if _m==1

drop if _m==2
drop _m

merge m:1 sa_finance1_cocode using "dta/firms_interest_coverage.dta"
keep if _m==3
drop _m

merge m:1 sa_finance1_cocode using "dta/industry.dta"
drop if _m==2
drop _m

summ num_of_modifications if period_new >= 28 & period_new <= 28
display r(sum)

gen num_of_modifications_dummy = 0
replace num_of_modifications_dummy = 1 if num_of_modifications > 0

gen num_of_modifications_dummy2 = 0
replace num_of_modifications_dummy2 = 1 if num_of_modifications2 > 0

egen firm_year = group(year sa_finance1_cocode)
egen firm_period = group(period_new  sa_finance1_cocode)
gen nic_prod_code_2 = substr(nic_prod_code,1,2)
gen nic_prod_code_3 = substr(nic_prod_code,1,3)

gen int_treat1_risky1 = treat1*risky_firm1

bysort creditorcode: egen first_period_ceo = min(period_new) if treat1 == 1
bysort creditorcode: egen min_first_period_ceo = min(first_period_ceo)

gen dist_from_ceo_change = period_new - min_first_period

gen dummy_pre1 = 0
replace dummy_pre1 = 1 if dist_from_ceo_change == -1 & dist_from_ceo_change != .
gen dummy_pre2 = 0
replace dummy_pre2 = 1 if dist_from_ceo_change == -2 & dist_from_ceo_change != .
gen dummy_pre3 = 0
replace dummy_pre3 = 1 if dist_from_ceo_change == -3 & dist_from_ceo_change != .
gen dummy_pre4 = 0
replace dummy_pre4 = 1 if dist_from_ceo_change == -4 & dist_from_ceo_change != .
gen dummy_pre5 = 0
replace dummy_pre5 = 1 if dist_from_ceo_change == -5 & dist_from_ceo_change != .
gen dummy_post = 0
replace dummy_post = 1 if dist_from_ceo_change >= 0 & dist_from_ceo_change != .

gen dummy_pre1_risky1 = dummy_pre1*risky_firm1
gen dummy_pre2_risky1 = dummy_pre2*risky_firm1
gen dummy_pre3_risky1 = dummy_pre3*risky_firm1
gen dummy_pre4_risky1 = dummy_pre4*risky_firm1
gen dummy_pre5_risky1 = dummy_pre5*risky_firm1
gen dummy_post_risky1 = dummy_post*risky_firm1

summ num_of_modifications_dummy if period_new  >= 28 & period_new  <= 33, d

merge m:1 creditorcode year quarter using "dta/bank_info.dta"
drop if _m==2
drop _m

merge m:1 creditorcode year using "dta/capital_adequacy_ratio.dta"
drop if _m==2
drop _m

gen log_income_year = log(1 + sa_total_income)
gen pat_income = sa_pat/sa_total_income
gen gnpa_close_loan_adv = sa_bd_gross_npa_closing_bal/sa_total_loans_and_advances

keep if period_new >= 28 & period_new <= 33

gen crisis = 1

save "dta/restructuring_trimmed.dta", replace

****************************************************************************************************************************************************
/* Creating restructuring data and regression table (Pre-crisis) */
****************************************************************************************************************************************************

use "dta/MCA_data_psb_year_quarter_final_pre_crisis.dta", replace

merge m:1 sa_finance1_cocode creditorcode using "dta/prev_yrs_loan_firms_quarterly_pre_crisis.dta"

keep if _m==3
drop _m

merge m:1 creditorcode using "dta/ceo_change_quarterly_pre_crisis.dta"

rename period_ceo period_ceo_ext

replace period_ceo_ext = 10000 if _m==1

drop if _m==2 
drop _m

gen treat1 = 0
replace treat1 = 1 if period_new >= period_ceo_ext

gen treat2 = 0
replace treat2 = 1 if period_new > period_ceo_ext

bysort creditorcode: egen sum_treat1 = sum(treat1)

merge m:1 sa_finance1_cocode creditorcode year quarter using "dta/restructuring_psb_final_pre_crisis.dta"

replace num_of_modifications = 0 if _m==1
replace num_of_modifications2 = 0 if _m==1

drop if _m==2
drop _m

merge m:1 sa_finance1_cocode using "dta/firms_interest_coverage_pre_crisis.dta"
keep if _m==3
drop _m

merge m:1 sa_finance1_cocode using "dta/industry.dta"
drop if _m==2
drop _m

summ num_of_modifications if period_new >= 28 & period_new <= 28
display r(sum)

gen num_of_modifications_dummy = 0
replace num_of_modifications_dummy = 1 if num_of_modifications > 0

gen num_of_modifications_dummy2 = 0
replace num_of_modifications_dummy2 = 1 if num_of_modifications2 > 0

egen firm_year = group(year sa_finance1_cocode)
egen firm_period = group(period_new  sa_finance1_cocode)
gen nic_prod_code_2 = substr(nic_prod_code,1,2)
gen nic_prod_code_3 = substr(nic_prod_code,1,3)

gen int_treat1_risky1 = treat1*risky_firm1

bysort creditorcode: egen first_period_ceo = min(period_new) if treat1 == 1
bysort creditorcode: egen min_first_period_ceo = min(first_period_ceo)

gen dist_from_ceo_change = period_new - min_first_period

gen dummy_pre1 = 0
replace dummy_pre1 = 1 if dist_from_ceo_change == -1 & dist_from_ceo_change != .
gen dummy_pre2 = 0
replace dummy_pre2 = 1 if dist_from_ceo_change == -2 & dist_from_ceo_change != .
gen dummy_pre3 = 0
replace dummy_pre3 = 1 if dist_from_ceo_change == -3 & dist_from_ceo_change != .
gen dummy_pre4 = 0
replace dummy_pre4 = 1 if dist_from_ceo_change == -4 & dist_from_ceo_change != .
gen dummy_pre5 = 0
replace dummy_pre5 = 1 if dist_from_ceo_change == -5 & dist_from_ceo_change != .
gen dummy_post = 0
replace dummy_post = 1 if dist_from_ceo_change >= 0 & dist_from_ceo_change != .

gen dummy_pre1_risky1 = dummy_pre1*risky_firm1
gen dummy_pre2_risky1 = dummy_pre2*risky_firm1
gen dummy_pre3_risky1 = dummy_pre3*risky_firm1
gen dummy_pre4_risky1 = dummy_pre4*risky_firm1
gen dummy_pre5_risky1 = dummy_pre5*risky_firm1
gen dummy_post_risky1 = dummy_post*risky_firm1

summ num_of_modifications_dummy if period_new  >= 28 & period_new  <= 33, d

merge m:1 creditorcode year quarter using "dta/bank_info.dta"
drop if _m==2
drop _m

merge m:1 creditorcode year using "dta/capital_adequacy_ratio.dta"
drop if _m==2
drop _m

gen log_income_year = log(1 + sa_total_income)
gen pat_income = sa_pat/sa_total_income
gen gnpa_close_loan_adv = sa_bd_gross_npa_closing_bal/sa_total_loans_and_advances

keep if period_new >= 16 & period_new <= 27

gen crisis = 0

save "dta/restructuring_trimmed_pre_crisis.dta", replace

************************************************************ Pre - Period Data Making (End) ************************************************************



****************************************************************************************************************************************************
/* Table 3 - Main Regression (Loan Dummy)*/
****************************************************************************************************************************************************

use "dta/tables.dta", replace

drop int_treat1_risky1

replace treat1 = 0 if creditorcode == 236337

gen int_treat1_risky1 = treat1*risky_firm1

// Bank*Firm groups
egen bank_firm = group(creditorcode sa_finance1_cocode)

// Bank*Time groups
egen bank_quarter = group(creditorcode period_new)

gen firmCode = sa_finance1_cocode
gen creditorCode = creditorcode
merge m:1 firmCode creditorCode year using "dta/FirmBankYearControls_2018 - Copy.dta"
drop if _m == 2
drop _m

merge m:1 sa_finance1_cocode year using "dta/firm_year_controls.dta"
drop if _m == 2
drop _m

gen lag_amount = exp(lag_lg_amount)
gen lag_amount_assets = lag_amount/(sa_total_assets*1000000)

reghdfe total_loan_given_dummy int_treat1_risky1 treat1 risky_firm1 avg_loan_duration loan_completed_5 lag_amount_assets if period_new >= 28 & period_new <= 33, absorb(creditorcode firm_year) cluster(nic_prod_code)
outreg2 using "Referee/tables/table3_main_reg.xls", replace label ctitle (Loan Dummy) dec(4) se coefastr bracket nocons addtext (Controls, Yes, Bank Fixed Effects, Yes, Bank x Quarter Fixed Effects, No, Firm x Year Fixed Effects, Yes, Bank-Firm Fixed Effects, Yes)

reghdfe total_loan_given_dummy int_treat1_risky1 treat1 risky_firm1 avg_loan_duration loan_completed_5 lag_amount_assets if period_new >= 28 & period_new <= 33, absorb(bank_quarter firm_year) cluster(nic_prod_code)
outreg2 using "Referee/tables/table3_main_reg.xls", append label ctitle (Loan Dummy) dec(4) se coefastr bracket nocons addtext (Controls, Yes, Bank Fixed Effects, No, Bank x Quarter Fixed Effects, Yes, Firm x Year Fixed Effects, Yes, Bank-Firm Fixed Effects, Yes)

reghdfe total_loan_given_dummy int_treat1_risky1 treat1 risky_firm1 avg_loan_duration loan_completed_5 lag_amount_assets if period_new >= 28 & period_new <= 33, absorb(bank_firm bank_quarter firm_year) cluster(nic_prod_code)
outreg2 using "Referee/tables/table3_main_reg.xls", append label ctitle (Loan Amount) dec(4) se coefastr bracket nocons addtext (Controls, Yes, Bank Fixed Effects, No, Bank-Quarter Fixed Effects, Yes, Firm x Year Fixed Effects, Yes, Bank-Firm Fixed Effects, Yes)

****************************************************************************************************************************************************
/* Table A3 - Pre-trends (Loan Dummy)*/
****************************************************************************************************************************************************

reghdfe total_loan_given_dummy dummy_post_risky1 dummy_pre3_risky1 dummy_pre4_risky1 dummy_pre5_risky1 treat1 risky_firm1 avg_loan_duration loan_completed_5 lag_amount_assets if period_new >= 24 & period_new <= 33 & dist_from_ceo_change >= -5, absorb(creditorcode firm_year) cluster(nic_prod_code)
outreg2 using "Referee/tables/table5_pre_trends.xls", replace label ctitle (Loan Dummy) dec(4) se coefastr bracket nocons addtext (Controls, Yes, Bank Fixed Effects, Yes, Bank x Quarter Fixed Effects, No, Firm x Year Fixed Effects, Yes, Bank-Firm Fixed Effects, No)

reghdfe total_loan_given_dummy dummy_post_risky1 dummy_pre3_risky1 dummy_pre4_risky1 dummy_pre5_risky1 treat1 risky_firm1 avg_loan_duration loan_completed_5 lag_amount_assets if period_new >= 24 & period_new <= 33 & dist_from_ceo_change >= -5, absorb(bank_quarter firm_year) cluster(nic_prod_code)
outreg2 using "Referee/tables/table5_pre_trends.xls", append label ctitle (Loan Dummy) dec(4) se coefastr bracket nocons addtext (Controls, Yes, Bank Fixed Effects, No, Bank x Quarter Fixed Effects, Yes, Firm x Year Fixed Effects, Yes, Bank-Firm Fixed Effects, No)

reghdfe total_loan_given_dummy dummy_post_risky1 dummy_pre3_risky1 dummy_pre4_risky1 dummy_pre5_risky1 treat1 risky_firm1 avg_loan_duration loan_completed_5 lag_amount_assets if period_new >= 24 & period_new <= 33 & dist_from_ceo_change >= -5, absorb(bank_quarter firm_year bank_firm) cluster(nic_prod_code)
outreg2 using "Referee/tables/table5_pre_trends.xls", append label ctitle (Loan Dummy) dec(4) se coefastr bracket nocons addtext (Controls, Yes, Bank Fixed Effects, No, Bank x Quarter Fixed Effects, Yes, Firm x Year Fixed Effects, Yes, Bank-Firm Fixed Effects, Yes)

****************************************************************************************************************************************************
/* Table 3 - Main Regression (Restructuring) */
****************************************************************************************************************************************************

use "dta/MCA_data_psb_year_quarter_final.dta", replace

merge m:1 sa_finance1_cocode creditorcode using "dta/prev_yrs_loan_firms_quarterly.dta"

keep if _m==3
drop _m

merge m:1 creditorcode using "dta/ceo_change_quarterly.dta"

replace period_ceo = 10000 if _m==1

drop if _m==2 
drop _m

gen treat1 = 0
replace treat1 = 1 if period_new >= period_ceo

gen treat2 = 0
replace treat2 = 1 if period_new > period_ceo

bysort creditorcode: egen sum_treat1 = sum(treat1)

merge m:1 sa_finance1_cocode creditorcode year quarter using "dta/restructuring_psb_final.dta"

replace num_of_modifications = 0 if _m==1
replace num_of_modifications2 = 0 if _m==1

drop if _m==2
drop _m

merge m:1 sa_finance1_cocode using "dta/firms_interest_coverage.dta"
keep if _m==3
drop _m

merge m:1 sa_finance1_cocode using "dta/industry.dta"
drop if _m==2
drop _m

summ num_of_modifications if period_new >= 28 & period_new <= 28
display r(sum)

gen num_of_modifications_dummy = 0
replace num_of_modifications_dummy = 1 if num_of_modifications > 0

gen num_of_modifications_dummy2 = 0
replace num_of_modifications_dummy2 = 1 if num_of_modifications2 > 0

egen firm_year = group(year sa_finance1_cocode)
egen firm_period = group(period_new  sa_finance1_cocode)
gen nic_prod_code_2 = substr(nic_prod_code,1,2)
gen nic_prod_code_3 = substr(nic_prod_code,1,3)

replace treat1 = 0 if creditorcode == 236337

gen int_treat1_risky1 = treat1*risky_firm1

bysort creditorcode: egen first_period_ceo = min(period_new) if treat1 == 1
bysort creditorcode: egen min_first_period_ceo = min(first_period_ceo)

gen dist_from_ceo_change = period_new - min_first_period

gen dummy_pre1 = 0
replace dummy_pre1 = 1 if dist_from_ceo_change == -1 & dist_from_ceo_change != .
gen dummy_pre2 = 0
replace dummy_pre2 = 1 if dist_from_ceo_change == -2 & dist_from_ceo_change != .
gen dummy_pre3 = 0
replace dummy_pre3 = 1 if dist_from_ceo_change == -3 & dist_from_ceo_change != .
gen dummy_pre4 = 0
replace dummy_pre4 = 1 if dist_from_ceo_change == -4 & dist_from_ceo_change != .
gen dummy_pre5 = 0
replace dummy_pre5 = 1 if dist_from_ceo_change == -5 & dist_from_ceo_change != .
gen dummy_post = 0
replace dummy_post = 1 if dist_from_ceo_change >= 0 & dist_from_ceo_change != .

gen dummy_pre1_risky1 = dummy_pre1*risky_firm1
gen dummy_pre2_risky1 = dummy_pre2*risky_firm1
gen dummy_pre3_risky1 = dummy_pre3*risky_firm1
gen dummy_pre4_risky1 = dummy_pre4*risky_firm1
gen dummy_pre5_risky1 = dummy_pre5*risky_firm1
gen dummy_post_risky1 = dummy_post*risky_firm1

summ num_of_modifications_dummy if period_new  >= 28 & period_new  <= 33, d

merge m:1 creditorcode year quarter using "dta/bank_info.dta"
drop if _m==2
drop _m

merge m:1 creditorcode year using "dta/capital_adequacy_ratio.dta"
drop if _m==2
drop _m

gen log_income_year = log(1 + sa_total_income)
gen pat_income = sa_pat/sa_total_income
gen gnpa_close_loan_adv = sa_bd_gross_npa_closing_bal/sa_total_loans_and_advances

// Bank*Firm groups
egen bank_firm = group(creditorcode sa_finance1_cocode)

// Bank*Time groups
egen bank_quarter = group(creditorcode period_new)

gen firmCode = sa_finance1_cocode
gen creditorCode = creditorcode
merge m:1 firmCode creditorCode year using "dta/FirmBankYearControls_2018 - Copy.dta"
drop if _m == 2
drop _m

merge m:1 sa_finance1_cocode year using "dta/firm_year_controls.dta"
drop if _m == 2
drop _m

gen lag_amount = exp(lag_lg_amount)
gen lag_amount_assets = lag_amount/(sa_total_assets*1000000)

reghdfe num_of_modifications_dummy int_treat1_risky1 treat1 risky_firm1 avg_loan_duration loan_completed_5 lag_amount_assets if period_new >= 28 & period_new <= 33, absorb(creditorcode firm_year) cluster(nic_prod_code)
outreg2 using "Referee/tables/table3_main_reg.xls", append label ctitle (Restructuring) dec(4) se coefastr bracket nocons addtext (Controls, Yes, Bank Fixed Effects, Yes, Bank x Quarter Fixed Effects, No, Firm x Year Fixed Effects, Yes, Bank-Firm Fixed Effects, Yes)

reghdfe num_of_modifications_dummy int_treat1_risky1 treat1 risky_firm1 avg_loan_duration loan_completed_5 lag_amount_assets if period_new >= 28 & period_new <= 33, absorb(bank_quarter firm_year) cluster(nic_prod_code)
outreg2 using "Referee/tables/table3_main_reg.xls", append label ctitle (Restructuring) dec(4) se coefastr bracket nocons addtext (Controls, Yes, Bank Fixed Effects, No, Bank x Quarter Fixed Effects, Yes, Firm x Year Fixed Effects, Yes, Bank-Firm Fixed Effects, Yes)

reghdfe num_of_modifications_dummy int_treat1_risky1 treat1 risky_firm1 avg_loan_duration loan_completed_5 lag_amount_assets if period_new >= 28 & period_new <= 33, absorb(bank_quarter firm_year bank_firm) cluster(nic_prod_code)
outreg2 using "Referee/tables/table3_main_reg.xls", append label ctitle (Restructuring) dec(4) se coefastr bracket nocons addtext (Controls, Yes, Bank Fixed Effects, No, Bank-Quarter Fixed Effects, Yes, Firm x Year Fixed Effects, Yes, Bank-Firm Fixed Effects, Yes)

****************************************************************************************************************************************************
/* Table A3 - Pre-trends (Restructuring)*/
****************************************************************************************************************************************************

reghdfe num_of_modifications_dummy dummy_post_risky1 dummy_pre3_risky1 dummy_pre4_risky1 dummy_pre5_risky1 treat1 risky_firm1 avg_loan_duration loan_completed_5 lag_amount_assets if period_new >= 24 & period_new <= 33 & dist_from_ceo_change >= -5, absorb(creditorcode firm_year) cluster(nic_prod_code)
outreg2 using "Referee/tables/table5_pre_trends.xls", append label ctitle (Restructuring) dec(4) se coefastr bracket nocons addtext (Controls, Yes, Bank Fixed Effects, Yes, Bank x Quarter Fixed Effects, No, Firm x Year Fixed Effects, Yes, Bank-Firm Fixed Effects, No)

reghdfe num_of_modifications_dummy dummy_post_risky1 dummy_pre3_risky1 dummy_pre4_risky1 dummy_pre5_risky1 treat1 risky_firm1 avg_loan_duration loan_completed_5 lag_amount_assets if period_new >= 24 & period_new <= 33 & dist_from_ceo_change >= -5, absorb(bank_quarter firm_year) cluster(nic_prod_code)
outreg2 using "Referee/tables/table5_pre_trends.xls", append label ctitle (Restructuring) dec(4) se coefastr bracket nocons addtext (Controls, Yes, Bank Fixed Effects, No, Bank x Quarter Fixed Effects, Yes, Firm x Year Fixed Effects, Yes, Bank-Firm Fixed Effects, No)

reghdfe num_of_modifications_dummy dummy_post_risky1 dummy_pre3_risky1 dummy_pre4_risky1 dummy_pre5_risky1 treat1 risky_firm1 avg_loan_duration loan_completed_5 lag_amount_assets if period_new >= 24 & period_new <= 33 & dist_from_ceo_change >= -5, absorb(bank_quarter firm_year bank_firm) cluster(nic_prod_code)
outreg2 using "Referee/tables/table5_pre_trends.xls", append label ctitle (Restructuring) dec(4) se coefastr bracket nocons addtext (Controls, Yes, Bank Fixed Effects, No, Bank x Quarter Fixed Effects, Yes, Firm x Year Fixed Effects, Yes, Bank-Firm Fixed Effects, Yes)

****************************************************************************************************************************************************
/* Table 4 - Triple Interaction Crisis (Loan Dummy) */
***************************************************************************************************************************************************

use "dta/tables_trimmed.dta", replace

append using "dta/tables_pre_crisis_trimmed.dta"

gen int_treat1_risky1_crisis = treat1*risky_firm1*crisis
gen int_treat1_crisis = treat1*crisis
gen int_risky1_crisis = risky_firm1*crisis

// Bank*Firm groups
egen bank_firm = group(creditorcode sa_finance1_cocode)

// Bank*Time groups
egen bank_quarter = group(creditorcode period_new)

gen firmCode = sa_finance1_cocode
gen creditorCode = creditorcode
merge m:1 firmCode creditorCode year using "dta/FirmBankYearControls_2018 - Copy.dta"
drop if _m == 2
drop _m

merge m:1 sa_finance1_cocode year using "dta/firm_year_controls.dta"
drop if _m == 2
drop _m

gen lag_amount = exp(lag_lg_amount)
gen lag_amount_assets = lag_amount/(sa_total_assets*1000000)

reghdfe total_loan_given_dummy int_treat1_risky1_crisis int_treat1_risky1 treat1 risky_firm1 int_treat1_crisis int_risky1_crisis crisis avg_loan_duration loan_completed_5 lag_amount_assets, absorb(bank_quarter firm_year bank_firm) cluster(nic_prod_code)
outreg2 using "Referee/tables/table6_triple_interaction.xls", replace label ctitle (Loan Dummy) dec(4) se coefastr bracket nocons addtext (Controls, Yes, Bank-Quarter Fixed Effects, Yes, Firm x Year Fixed Effects, Yes, Bank-Firm Fixed Effects, Yes)

****************************************************************************************************************************************************
/* Table 4 - Triple Interaction Crisis (Restructuring) */
****************************************************************************************************************************************************

use "dta/restructuring_trimmed.dta", replace
append using "dta/restructuring_trimmed_pre_crisis.dta"

gen int_treat1_risky1_crisis = treat1*risky_firm1*crisis
gen int_treat1_crisis = treat1*crisis
gen int_risky1_crisis = risky_firm1*crisis

// Bank*Firm groups
egen bank_firm = group(creditorcode sa_finance1_cocode)

// Bank*Time groups
egen bank_quarter = group(creditorcode period_new)

gen firmCode = sa_finance1_cocode
gen creditorCode = creditorcode
merge m:1 firmCode creditorCode year using "dta/FirmBankYearControls_2018 - Copy.dta"
drop if _m == 2
drop _m

merge m:1 sa_finance1_cocode year using "dta/firm_year_controls.dta"
drop if _m == 2
drop _m

gen lag_amount = exp(lag_lg_amount)
gen lag_amount_assets = lag_amount/(sa_total_assets*1000000)

reghdfe num_of_modifications_dummy int_treat1_risky1_crisis int_treat1_risky1  treat1 risky_firm1 int_treat1_crisis int_risky1_crisis crisis avg_loan_duration loan_completed_5 lag_amount_assets , absorb(bank_quarter firm_year bank_firm) cluster(nic_prod_code)
outreg2 using "Referee/tables/table6_triple_interaction.xls", append label ctitle (Restructuring) dec(4) se coefastr bracket nocons addtext (Controls, Yes, Bank-Quarter Fixed Effects, Yes, Firm x Year Fixed Effects, Yes, Bank-Firm Fixed Effects, Yes)



********************************************************* Data Creation - Table 4 (Industry Crisis) (Begin) ********************************************

import delimited "raw\firms_interest_coverage.txt", delimiter("|") clear

destring sa_total_income-sa_debt_equity, force replace

tostring sa_finance1_year, force replace
gen day = substr(sa_finance1_year,7,2)
gen month = substr(sa_finance1_year,5,2)
gen year = substr(sa_finance1_year,1,4)

destring day-year, force replace

duplicates tag sa_finance1_cocode year, gen(x)
bysort sa_finance1_cocode: gen march = 1 if x>0 & month == 3
bysort sa_finance1_cocode: egen max_march = max(march) if x>0
drop if x>0 & max_march == 1 & march != 1
sort sa_finance1_cocode year month
bysort sa_finance1_cocode year: gen dup_n = _n if x > 0 & max_march != 1 
drop if dup_n > 1 & dup_n != .
sort sa_finance1_cocode year month day
bysort sa_finance1_cocode year: egen march_dup = sum(march) if x > 0 & max_march == 1 
bysort sa_finance1_cocode year: gen march_dup_n = _n if march_dup > 1 & march_dup != .
bysort sa_finance1_cocode year: gen march_dup_total_n = _N if march_dup > 1 & march_dup != .
drop if march_dup_n < march_dup_total_n & march_dup_n != .

drop x-march_dup_total_n

merge m:1 sa_finance1_cocode using "dta/industry_code.dta"
keep if _m==3
drop _m

save "dta/alternate_risk_criteria_data_interim1.dta", replace

****************************************************************************************************************************************************
/* Creating balanced panel from firm-bank pair (quarter-year)*/
****************************************************************************************************************************************************

use "dta/MCA_data_psb_only_quarter.dta", replace
append using "dta/MCA_data_psb_only_quarter.dta"
append using "dta/MCA_data_psb_only_quarter.dta"
append using "dta/MCA_data_psb_only_quarter.dta"
append using "dta/MCA_data_psb_only_quarter.dta"
append using "dta/MCA_data_psb_only_quarter.dta"
append using "dta/MCA_data_psb_only_quarter.dta"
append using "dta/MCA_data_psb_only_quarter.dta"
append using "dta/MCA_data_psb_only_quarter.dta"
append using "dta/MCA_data_psb_only_quarter.dta"
append using "dta/MCA_data_psb_only_quarter.dta"
append using "dta/MCA_data_psb_only_quarter.dta"
append using "dta/MCA_data_psb_only_quarter.dta"
append using "dta/MCA_data_psb_only_quarter.dta"

bysort creditorcode firmcode quarter: gen year = _n

replace year = year + 2001

save "dta/MCA_data_psb_year_quarter_blank_industry_crisis.dta", replace

****************************************************************************************************************************************************
/* Giving quarter-year values to the original data */
****************************************************************************************************************************************************

use "dta/MCA_data_with_dates.dta", replace

keep if sanctionDate >= td(01apr2001) & sanctionDate <= td(31mar2015)

rename (creditorCode firmCode) (creditorcode firmcode)

merge m:1 creditorcode using "dta/public_sector_banks1.dta"

keep if _m==3
drop _m

gen period = .

replace period = 1 if sanctionDate >= td(01apr2001) & sanctionDate <= td(30jun2001)
replace period = 2 if sanctionDate >= td(01jul2001) & sanctionDate <= td(30sep2001)
replace period = 3 if sanctionDate >= td(01oct2001) & sanctionDate <= td(31dec2001)
replace period = 4 if sanctionDate >= td(01jan2002) & sanctionDate <= td(31mar2002)
replace period = 5 if sanctionDate >= td(01apr2002) & sanctionDate <= td(30jun2002)
replace period = 6 if sanctionDate >= td(01jul2002) & sanctionDate <= td(30sep2002)
replace period = 7 if sanctionDate >= td(01oct2002) & sanctionDate <= td(31dec2002)
replace period = 8 if sanctionDate >= td(01jan2003) & sanctionDate <= td(31mar2003)
replace period = 9 if sanctionDate >= td(01apr2003) & sanctionDate <= td(30jun2003)
replace period = 10 if sanctionDate >= td(01jul2003) & sanctionDate <= td(30sep2003)
replace period = 11 if sanctionDate >= td(01oct2003) & sanctionDate <= td(31dec2003)
replace period = 12 if sanctionDate >= td(01jan2004) & sanctionDate <= td(31mar2004)
replace period = 13 if sanctionDate >= td(01apr2004) & sanctionDate <= td(30jun2004)
replace period = 14 if sanctionDate >= td(01jul2004) & sanctionDate <= td(30sep2004)
replace period = 15 if sanctionDate >= td(01oct2004) & sanctionDate <= td(31dec2004)
replace period = 16 if sanctionDate >= td(01jan2005) & sanctionDate <= td(31mar2005)
replace period = 17 if sanctionDate >= td(01apr2005) & sanctionDate <= td(30jun2005)
replace period = 18 if sanctionDate >= td(01jul2005) & sanctionDate <= td(30sep2005)
replace period = 19 if sanctionDate >= td(01oct2005) & sanctionDate <= td(31dec2005)
replace period = 20 if sanctionDate >= td(01jan2006) & sanctionDate <= td(31mar2006)
replace period = 21 if sanctionDate >= td(01apr2006) & sanctionDate <= td(30jun2006)
replace period = 22 if sanctionDate >= td(01jul2006) & sanctionDate <= td(30sep2006)
replace period = 23 if sanctionDate >= td(01oct2006) & sanctionDate <= td(31dec2006)
replace period = 24 if sanctionDate >= td(01jan2007) & sanctionDate <= td(31mar2007)
replace period = 25 if sanctionDate >= td(01apr2007) & sanctionDate <= td(30jun2007)
replace period = 26 if sanctionDate >= td(01jul2007) & sanctionDate <= td(30sep2007)
replace period = 27 if sanctionDate >= td(01oct2007) & sanctionDate <= td(31dec2007)
replace period = 28 if sanctionDate >= td(01jan2008) & sanctionDate <= td(31mar2008)
replace period = 29 if sanctionDate >= td(01apr2008) & sanctionDate <= td(30jun2008)
replace period = 30 if sanctionDate >= td(01jul2008) & sanctionDate <= td(30sep2008)
replace period = 31 if sanctionDate >= td(01oct2008) & sanctionDate <= td(31dec2008)
replace period = 32 if sanctionDate >= td(01jan2009) & sanctionDate <= td(31mar2009)
replace period = 33 if sanctionDate >= td(01apr2009) & sanctionDate <= td(30jun2009)
replace period = 34 if sanctionDate >= td(01jul2009) & sanctionDate <= td(30sep2009)
replace period = 35 if sanctionDate >= td(01oct2009) & sanctionDate <= td(31dec2009)
replace period = 36 if sanctionDate >= td(01jan2010) & sanctionDate <= td(31mar2010)
replace period = 37 if sanctionDate >= td(01apr2010) & sanctionDate <= td(30jun2010)
replace period = 38 if sanctionDate >= td(01jul2010) & sanctionDate <= td(30sep2010)
replace period = 39 if sanctionDate >= td(01oct2010) & sanctionDate <= td(31dec2010)
replace period = 40 if sanctionDate >= td(01jan2011) & sanctionDate <= td(31mar2011)
replace period = 41 if sanctionDate >= td(01apr2011) & sanctionDate <= td(30jun2011)
replace period = 42 if sanctionDate >= td(01jul2011) & sanctionDate <= td(30sep2011)
replace period = 43 if sanctionDate >= td(01oct2011) & sanctionDate <= td(31dec2011)
replace period = 44 if sanctionDate >= td(01jan2012) & sanctionDate <= td(31mar2012)
replace period = 45 if sanctionDate >= td(01apr2012) & sanctionDate <= td(30jun2012)
replace period = 46 if sanctionDate >= td(01jul2012) & sanctionDate <= td(30sep2012)
replace period = 47 if sanctionDate >= td(01oct2012) & sanctionDate <= td(31dec2012)
replace period = 48 if sanctionDate >= td(01jan2013) & sanctionDate <= td(31mar2013)
replace period = 49 if sanctionDate >= td(01apr2013) & sanctionDate <= td(30jun2013)
replace period = 50 if sanctionDate >= td(01jul2013) & sanctionDate <= td(30sep2013)
replace period = 51 if sanctionDate >= td(01oct2013) & sanctionDate <= td(31dec2013)
replace period = 52 if sanctionDate >= td(01jan2014) & sanctionDate <= td(31mar2014)
replace period = 53 if sanctionDate >= td(01apr2014) & sanctionDate <= td(30jun2014)
replace period = 54 if sanctionDate >= td(01jul2014) & sanctionDate <= td(30sep2014)
replace period = 55 if sanctionDate >= td(01oct2014) & sanctionDate <= td(31dec2014)
replace period = 56 if sanctionDate >= td(01jan2015) & sanctionDate <= td(31mar2015)

gen period_repayment = .

replace period_repayment = 1 if repaymentDate >= td(01apr2001) & repaymentDate <= td(30jun2001)
replace period_repayment = 2 if repaymentDate >= td(01jul2001) & repaymentDate <= td(30sep2001)
replace period_repayment = 3 if repaymentDate >= td(01oct2001) & repaymentDate <= td(31dec2001)
replace period_repayment = 4 if repaymentDate >= td(01jan2002) & repaymentDate <= td(31mar2002)
replace period_repayment = 5 if repaymentDate >= td(01apr2002) & repaymentDate <= td(30jun2002)
replace period_repayment = 6 if repaymentDate >= td(01jul2002) & repaymentDate <= td(30sep2002)
replace period_repayment = 7 if repaymentDate >= td(01oct2002) & repaymentDate <= td(31dec2002)
replace period_repayment = 8 if repaymentDate >= td(01jan2003) & repaymentDate <= td(31mar2003)
replace period_repayment = 9 if repaymentDate >= td(01apr2003) & repaymentDate <= td(30jun2003)
replace period_repayment = 10 if repaymentDate >= td(01jul2003) & repaymentDate <= td(30sep2003)
replace period_repayment = 11 if repaymentDate >= td(01oct2003) & repaymentDate <= td(31dec2003)
replace period_repayment = 12 if repaymentDate >= td(01jan2004) & repaymentDate <= td(31mar2004)
replace period_repayment = 13 if repaymentDate >= td(01apr2004) & repaymentDate <= td(30jun2004)
replace period_repayment = 14 if repaymentDate >= td(01jul2004) & repaymentDate <= td(30sep2004)
replace period_repayment = 15 if repaymentDate >= td(01oct2004) & repaymentDate <= td(31dec2004)
replace period_repayment = 16 if repaymentDate >= td(01jan2005) & repaymentDate <= td(31mar2005)
replace period_repayment = 17 if repaymentDate >= td(01apr2005) & repaymentDate <= td(30jun2005)
replace period_repayment = 18 if repaymentDate >= td(01jul2005) & repaymentDate <= td(30sep2005)
replace period_repayment = 19 if repaymentDate >= td(01oct2005) & repaymentDate <= td(31dec2005)
replace period_repayment = 20 if repaymentDate >= td(01jan2006) & repaymentDate <= td(31mar2006)
replace period_repayment = 21 if repaymentDate >= td(01apr2006) & repaymentDate <= td(30jun2006)
replace period_repayment = 22 if repaymentDate >= td(01jul2006) & repaymentDate <= td(30sep2006)
replace period_repayment = 23 if repaymentDate >= td(01oct2006) & repaymentDate <= td(31dec2006)
replace period_repayment = 24 if repaymentDate >= td(01jan2007) & repaymentDate <= td(31mar2007)
replace period_repayment = 25 if repaymentDate >= td(01apr2007) & repaymentDate <= td(30jun2007)
replace period_repayment = 26 if repaymentDate >= td(01jul2007) & repaymentDate <= td(30sep2007)
replace period_repayment = 27 if repaymentDate >= td(01oct2007) & repaymentDate <= td(31dec2007)
replace period_repayment = 28 if repaymentDate >= td(01jan2008) & repaymentDate <= td(31mar2008)
replace period_repayment = 29 if repaymentDate >= td(01apr2008) & repaymentDate <= td(30jun2008)
replace period_repayment = 30 if repaymentDate >= td(01jul2008) & repaymentDate <= td(30sep2008)
replace period_repayment = 31 if repaymentDate >= td(01oct2008) & repaymentDate <= td(31dec2008)
replace period_repayment = 32 if repaymentDate >= td(01jan2009) & repaymentDate <= td(31mar2009)
replace period_repayment = 33 if repaymentDate >= td(01apr2009) & repaymentDate <= td(30jun2009)
replace period_repayment = 34 if repaymentDate >= td(01jul2009) & repaymentDate <= td(30sep2009)
replace period_repayment = 35 if repaymentDate >= td(01oct2009) & repaymentDate <= td(31dec2009)
replace period_repayment = 36 if repaymentDate >= td(01jan2010) & repaymentDate <= td(31mar2010)
replace period_repayment = 37 if repaymentDate >= td(01apr2010) & repaymentDate <= td(30jun2010)
replace period_repayment = 38 if repaymentDate >= td(01jul2010) & repaymentDate <= td(30sep2010)
replace period_repayment = 39 if repaymentDate >= td(01oct2010) & repaymentDate <= td(31dec2010)
replace period_repayment = 40 if repaymentDate >= td(01jan2011) & repaymentDate <= td(31mar2011)
replace period_repayment = 41 if repaymentDate >= td(01apr2011) & repaymentDate <= td(30jun2011)
replace period_repayment = 42 if repaymentDate >= td(01jul2011) & repaymentDate <= td(30sep2011)
replace period_repayment = 43 if repaymentDate >= td(01oct2011) & repaymentDate <= td(31dec2011)
replace period_repayment = 44 if repaymentDate >= td(01jan2012) & repaymentDate <= td(31mar2012)
replace period_repayment = 45 if repaymentDate >= td(01apr2012) & repaymentDate <= td(30jun2012)
replace period_repayment = 46 if repaymentDate >= td(01jul2012) & repaymentDate <= td(30sep2012)
replace period_repayment = 47 if repaymentDate >= td(01oct2012) & repaymentDate <= td(31dec2012)
replace period_repayment = 48 if repaymentDate >= td(01jan2013) & repaymentDate <= td(31mar2013)
replace period_repayment = 49 if repaymentDate >= td(01apr2013) & repaymentDate <= td(30jun2013)
replace period_repayment = 50 if repaymentDate >= td(01jul2013) & repaymentDate <= td(30sep2013)
replace period_repayment = 51 if repaymentDate >= td(01oct2013) & repaymentDate <= td(31dec2013)
replace period_repayment = 52 if repaymentDate >= td(01jan2014) & repaymentDate <= td(31mar2014)
replace period_repayment = 53 if repaymentDate >= td(01apr2014) & repaymentDate <= td(30jun2014)
replace period_repayment = 54 if repaymentDate >= td(01jul2014) & repaymentDate <= td(30sep2014)
replace period_repayment = 55 if repaymentDate >= td(01oct2014) & repaymentDate <= td(31dec2014)
replace period_repayment = 56 if repaymentDate >= td(01jan2015) & repaymentDate <= td(31mar2015)

gen year = int((period-1)/4)
replace year = year + 2002
gen quarter = mod(period,4)
replace quarter = 4 if quarter == 0

gen zero_loan_amount = 0
replace zero_loan_amount = 1 if loanAmount == 0

bysort creditorcode firmcode year quarter: egen total_loan_amount = sum(loanAmount)
bysort creditorcode firmcode year quarter: egen total_loan_given = sum(psb)

duplicates drop creditorcode firmcode year quarter, force

save "dta/MCA_data_psb_quarter_included_industry_crisis.dta", replace

****************************************************************************************************************************************************
/* Creating restructuring data (modifications) */
****************************************************************************************************************************************************

use "raw\LoansInMCA(2).dta", replace

keep if sanctionDate  >= td(01apr2003) & sanctionDate  <= td(30sep2007)
keep if modificationDate  >= td(01apr2003) & modificationDate  <= td(31mar2014)

rename (creditorCode firmCode) (creditorcode firmcode)

merge m:1 creditorcode using "dta/public_sector_banks1.dta"

keep if _m==3
drop _m

gen period = .

replace period = 1 if modificationDate >= td(01apr2001) & modificationDate <= td(30jun2001)
replace period = 2 if modificationDate >= td(01jul2001) & modificationDate <= td(30sep2001)
replace period = 3 if modificationDate >= td(01oct2001) & modificationDate <= td(31dec2001)
replace period = 4 if modificationDate >= td(01jan2002) & modificationDate <= td(31mar2002)
replace period = 5 if modificationDate >= td(01apr2002) & modificationDate <= td(30jun2002)
replace period = 6 if modificationDate >= td(01jul2002) & modificationDate <= td(30sep2002)
replace period = 7 if modificationDate >= td(01oct2002) & modificationDate <= td(31dec2002)
replace period = 8 if modificationDate >= td(01jan2003) & modificationDate <= td(31mar2003)
replace period = 9 if modificationDate >= td(01apr2003) & modificationDate <= td(30jun2003)
replace period = 10 if modificationDate >= td(01jul2003) & modificationDate <= td(30sep2003)
replace period = 11 if modificationDate >= td(01oct2003) & modificationDate <= td(31dec2003)
replace period = 12 if modificationDate >= td(01jan2004) & modificationDate <= td(31mar2004)
replace period = 13 if modificationDate >= td(01apr2004) & modificationDate <= td(30jun2004)
replace period = 14 if modificationDate >= td(01jul2004) & modificationDate <= td(30sep2004)
replace period = 15 if modificationDate >= td(01oct2004) & modificationDate <= td(31dec2004)
replace period = 16 if modificationDate >= td(01jan2005) & modificationDate <= td(31mar2005)
replace period = 17 if modificationDate >= td(01apr2005) & modificationDate <= td(30jun2005)
replace period = 18 if modificationDate >= td(01jul2005) & modificationDate <= td(30sep2005)
replace period = 19 if modificationDate >= td(01oct2005) & modificationDate <= td(31dec2005)
replace period = 20 if modificationDate >= td(01jan2006) & modificationDate <= td(31mar2006)
replace period = 21 if modificationDate >= td(01apr2006) & modificationDate <= td(30jun2006)
replace period = 22 if modificationDate >= td(01jul2006) & modificationDate <= td(30sep2006)
replace period = 23 if modificationDate >= td(01oct2006) & modificationDate <= td(31dec2006)
replace period = 24 if modificationDate >= td(01jan2007) & modificationDate <= td(31mar2007)
replace period = 25 if modificationDate >= td(01apr2007) & modificationDate <= td(30jun2007)
replace period = 26 if modificationDate >= td(01jul2007) & modificationDate <= td(30sep2007)
replace period = 27 if modificationDate >= td(01oct2007) & modificationDate <= td(31dec2007)
replace period = 28 if modificationDate >= td(01jan2008) & modificationDate <= td(31mar2008)
replace period = 29 if modificationDate >= td(01apr2008) & modificationDate <= td(30jun2008)
replace period = 30 if modificationDate >= td(01jul2008) & modificationDate <= td(30sep2008)
replace period = 31 if modificationDate >= td(01oct2008) & modificationDate <= td(31dec2008)
replace period = 32 if modificationDate >= td(01jan2009) & modificationDate <= td(31mar2009)
replace period = 33 if modificationDate >= td(01apr2009) & modificationDate <= td(30jun2009)
replace period = 34 if modificationDate >= td(01jul2009) & modificationDate <= td(30sep2009)
replace period = 35 if modificationDate >= td(01oct2009) & modificationDate <= td(31dec2009)
replace period = 36 if modificationDate >= td(01jan2010) & modificationDate <= td(31mar2010)
replace period = 37 if modificationDate >= td(01apr2010) & modificationDate <= td(30jun2010)
replace period = 38 if modificationDate >= td(01jul2010) & modificationDate <= td(30sep2010)
replace period = 39 if modificationDate >= td(01oct2010) & modificationDate <= td(31dec2010)
replace period = 40 if modificationDate >= td(01jan2011) & modificationDate <= td(31mar2011)
replace period = 41 if modificationDate >= td(01apr2011) & modificationDate <= td(30jun2011)
replace period = 42 if modificationDate >= td(01jul2011) & modificationDate <= td(30sep2011)
replace period = 43 if modificationDate >= td(01oct2011) & modificationDate <= td(31dec2011)
replace period = 44 if modificationDate >= td(01jan2012) & modificationDate <= td(31mar2012)
replace period = 45 if modificationDate >= td(01apr2012) & modificationDate <= td(30jun2012)
replace period = 46 if modificationDate >= td(01jul2012) & modificationDate <= td(30sep2012)
replace period = 47 if modificationDate >= td(01oct2012) & modificationDate <= td(31dec2012)
replace period = 48 if modificationDate >= td(01jan2013) & modificationDate <= td(31mar2013)
replace period = 49 if modificationDate >= td(01apr2013) & modificationDate <= td(30jun2013)
replace period = 50 if modificationDate >= td(01jul2013) & modificationDate <= td(30sep2013)
replace period = 51 if modificationDate >= td(01oct2013) & modificationDate <= td(31dec2013)
replace period = 52 if modificationDate >= td(01jan2014) & modificationDate <= td(31mar2014)

gen period_sanction = .

replace period_sanction = 1 if sanctionDate >= td(01apr2001) & sanctionDate <= td(30jun2001)
replace period_sanction = 2 if sanctionDate >= td(01jul2001) & sanctionDate <= td(30sep2001)
replace period_sanction = 3 if sanctionDate >= td(01oct2001) & sanctionDate <= td(31dec2001)
replace period_sanction = 4 if sanctionDate >= td(01jan2002) & sanctionDate <= td(31mar2002)
replace period_sanction = 5 if sanctionDate >= td(01apr2002) & sanctionDate <= td(30jun2002)
replace period_sanction = 6 if sanctionDate >= td(01jul2002) & sanctionDate <= td(30sep2002)
replace period_sanction = 7 if sanctionDate >= td(01oct2002) & sanctionDate <= td(31dec2002)
replace period_sanction = 8 if sanctionDate >= td(01jan2003) & sanctionDate <= td(31mar2003)
replace period_sanction = 9 if sanctionDate >= td(01apr2003) & sanctionDate <= td(30jun2003)
replace period_sanction = 10 if sanctionDate >= td(01jul2003) & sanctionDate <= td(30sep2003)
replace period_sanction = 11 if sanctionDate >= td(01oct2003) & sanctionDate <= td(31dec2003)
replace period_sanction = 12 if sanctionDate >= td(01jan2004) & sanctionDate <= td(31mar2004)
replace period_sanction = 13 if sanctionDate >= td(01apr2004) & sanctionDate <= td(30jun2004)
replace period_sanction = 14 if sanctionDate >= td(01jul2004) & sanctionDate <= td(30sep2004)
replace period_sanction = 15 if sanctionDate >= td(01oct2004) & sanctionDate <= td(31dec2004)
replace period_sanction = 16 if sanctionDate >= td(01jan2005) & sanctionDate <= td(31mar2005)
replace period_sanction = 17 if sanctionDate >= td(01apr2005) & sanctionDate <= td(30jun2005)
replace period_sanction = 18 if sanctionDate >= td(01jul2005) & sanctionDate <= td(30sep2005)
replace period_sanction = 19 if sanctionDate >= td(01oct2005) & sanctionDate <= td(31dec2005)
replace period_sanction = 20 if sanctionDate >= td(01jan2006) & sanctionDate <= td(31mar2006)
replace period_sanction = 21 if sanctionDate >= td(01apr2006) & sanctionDate <= td(30jun2006)
replace period_sanction = 22 if sanctionDate >= td(01jul2006) & sanctionDate <= td(30sep2006)
replace period_sanction = 23 if sanctionDate >= td(01oct2006) & sanctionDate <= td(31dec2006)
replace period_sanction = 24 if sanctionDate >= td(01jan2007) & sanctionDate <= td(31mar2007)
replace period_sanction = 25 if sanctionDate >= td(01apr2007) & sanctionDate <= td(30jun2007)
replace period_sanction = 26 if sanctionDate >= td(01jul2007) & sanctionDate <= td(30sep2007)
replace period_sanction = 27 if sanctionDate >= td(01oct2007) & sanctionDate <= td(31dec2007)
replace period_sanction = 28 if sanctionDate >= td(01jan2008) & sanctionDate <= td(31mar2008)
replace period_sanction = 29 if sanctionDate >= td(01apr2008) & sanctionDate <= td(30jun2008)
replace period_sanction = 30 if sanctionDate >= td(01jul2008) & sanctionDate <= td(30sep2008)
replace period_sanction = 31 if sanctionDate >= td(01oct2008) & sanctionDate <= td(31dec2008)
replace period_sanction = 32 if sanctionDate >= td(01jan2009) & sanctionDate <= td(31mar2009)
replace period_sanction = 33 if sanctionDate >= td(01apr2009) & sanctionDate <= td(30jun2009)
replace period_sanction = 34 if sanctionDate >= td(01jul2009) & sanctionDate <= td(30sep2009)
replace period_sanction = 35 if sanctionDate >= td(01oct2009) & sanctionDate <= td(31dec2009)
replace period_sanction = 36 if sanctionDate >= td(01jan2010) & sanctionDate <= td(31mar2010)
replace period_sanction = 37 if sanctionDate >= td(01apr2010) & sanctionDate <= td(30jun2010)
replace period_sanction = 38 if sanctionDate >= td(01jul2010) & sanctionDate <= td(30sep2010)
replace period_sanction = 39 if sanctionDate >= td(01oct2010) & sanctionDate <= td(31dec2010)
replace period_sanction = 40 if sanctionDate >= td(01jan2011) & sanctionDate <= td(31mar2011)
replace period_sanction = 41 if sanctionDate >= td(01apr2011) & sanctionDate <= td(30jun2011)
replace period_sanction = 42 if sanctionDate >= td(01jul2011) & sanctionDate <= td(30sep2011)
replace period_sanction = 43 if sanctionDate >= td(01oct2011) & sanctionDate <= td(31dec2011)
replace period_sanction = 44 if sanctionDate >= td(01jan2012) & sanctionDate <= td(31mar2012)
replace period_sanction = 45 if sanctionDate >= td(01apr2012) & sanctionDate <= td(30jun2012)
replace period_sanction = 46 if sanctionDate >= td(01jul2012) & sanctionDate <= td(30sep2012)
replace period_sanction = 47 if sanctionDate >= td(01oct2012) & sanctionDate <= td(31dec2012)
replace period_sanction = 48 if sanctionDate >= td(01jan2013) & sanctionDate <= td(31mar2013)
replace period_sanction = 49 if sanctionDate >= td(01apr2013) & sanctionDate <= td(30jun2013)
replace period_sanction = 50 if sanctionDate >= td(01jul2013) & sanctionDate <= td(30sep2013)
replace period_sanction = 51 if sanctionDate >= td(01oct2013) & sanctionDate <= td(31dec2013)
replace period_sanction = 52 if sanctionDate >= td(01jan2014) & sanctionDate <= td(31mar2014)

gen year = int((period-1)/4)
replace year = year + 2002
gen quarter = mod(period,4)
replace quarter = 4 if quarter == 0

bysort creditorcode firmcode year quarter: gen num_of_modifications = _N

gen same_period_modification = 0
replace same_period_modification = 1 if period == period_sanction

bysort creditorcode firmcode year quarter: egen num_same_modifications = sum(same_period_modification)

gen num_of_modifications2 = num_of_modifications - num_same_modifications

duplicates drop creditorcode firmcode year quarter, force

rename firmcode sa_finance1_cocode

keep creditorcode sa_finance1_cocode year quarter period num_of_modifications num_of_modifications2 loanAmount

save "dta/restructuring_psb_final_extended_industry_crisis.dta", replace

****************************************************************************************************************************************************
/* Merging the originial data with balanced panel data since we have quarter-year values in original data */
****************************************************************************************************************************************************

use "dta/MCA_data_psb_year_quarter_blank_industry_crisis.dta", replace
merge 1:1 creditorcode firmcode year quarter using "dta/MCA_data_psb_quarter_included_industry_crisis.dta"

bysort creditorcode firmcode: egen first_sanction_period = min(period)
bysort creditorcode firmcode: egen last_repayment_period = max(period_repayment)

replace total_loan_amount = 0 if _m==1
replace total_loan_given = 0 if _m==1

drop _m

gen period_new = 4*(year-2002) + quarter

rename firmcode sa_finance1_cocode

save "dta/MCA_data_psb_year_quarter_final_industry_crisis.dta", replace

****************************************************************************************************************************************************
/* Create proxy for crisis variable */
****************************************************************************************************************************************************

use "dta/alternate_risk_criteria_data_interim1.dta", replace

merge m:1 sa_finance1_cocode using "dta/industry.dta"
gen nic_prod_code_2 = substr(nic_prod_code, 1, 2)

tsset sa_finance1_cocode year, yearly

gen firm_sales_growth = (sa_sales - L1.sa_sales)/L1.sa_sales

gen neg_sales_gr_dummy = .
replace neg_sales_gr_dummy = 0 if firm_sales_growth >= 0 & firm_sales_growth !=.
replace neg_sales_gr_dummy = 1 if firm_sales_growth < 0 & firm_sales_growth !=.

bysort nic_prod_code year: egen mean_neg_sales_gr = mean(neg_sales_gr_dummy)

gen ind_neg_sales_firm_med = .
replace ind_neg_sales_firm_med = 0 if mean_neg_sales_gr < 0.5 & mean_neg_sales_gr !=.
replace ind_neg_sales_firm_med = 1 if mean_neg_sales_gr >= 0.5 & mean_neg_sales_gr !=.


gen neg_pat_dummy = .
replace neg_pat_dummy = 0 if sa_pat >= 0 & sa_pat !=.
replace neg_pat_dummy = 1 if sa_pat < 0 & sa_pat !=.

bysort nic_prod_code year: egen mean_neg_pat = mean(neg_pat_dummy)

gen ind_neg_pat_firm_med = .
replace ind_neg_pat_firm_med = 0 if mean_neg_pat < 0.5 & mean_neg_pat !=.
replace ind_neg_pat_firm_med = 1 if mean_neg_pat >= 0.5 & mean_neg_pat !=.

gen neg_pbt_dummy = .
replace neg_pbt_dummy = 0 if sa_pbt >= 0 & sa_pbt !=.
replace neg_pbt_dummy = 1 if sa_pbt < 0 & sa_pbt !=.

bysort nic_prod_code year: egen mean_neg_pbt = mean(neg_pbt_dummy)

gen ind_neg_pbt_firm_med = .
replace ind_neg_pbt_firm_med = 0 if mean_neg_pbt < 0.5 & mean_neg_pbt !=.
replace ind_neg_pbt_firm_med = 1 if mean_neg_pbt >= 0.5 & mean_neg_pbt !=.

gen neg_pbit_dummy = .
replace neg_pbit_dummy = 0 if sa_pbit >= 0 & sa_pbit !=.
replace neg_pbit_dummy = 1 if sa_pbit < 0 & sa_pbit !=.

bysort nic_prod_code year: egen mean_neg_pbit = mean(neg_pbit_dummy)

gen ind_neg_pbit_firm_med = .
replace ind_neg_pbit_firm_med = 0 if mean_neg_pbit < 0.5 & mean_neg_pbit !=.
replace ind_neg_pbit_firm_med = 1 if mean_neg_pbit >= 0.5 & mean_neg_pbit !=.

gen neg_pbdita_dummy = .
replace neg_pbdita_dummy = 0 if sa_pbdita >= 0 & sa_pbdita !=.
replace neg_pbdita_dummy = 1 if sa_pbdita < 0 & sa_pbdita !=.

bysort nic_prod_code year: egen mean_neg_pbdita = mean(neg_pbdita_dummy)

gen ind_neg_pbdita_firm_med = .
replace ind_neg_pbdita_firm_med = 0 if mean_neg_pbdita < 0.5 & mean_neg_pbdita !=.
replace ind_neg_pbdita_firm_med = 1 if mean_neg_pbdita >= 0.5 & mean_neg_pbdita !=.

gen ind_neg_patsales_firm_med = .
replace ind_neg_patsales_firm_med = 0 if (mean_neg_pat < 0.5 | mean_neg_sales_gr < 0.5) & mean_neg_pat !=. & mean_neg_sales_gr !=.
replace ind_neg_patsales_firm_med = 1 if mean_neg_pat >= 0.5 & mean_neg_pat !=. & mean_neg_sales_gr >= 0.5 & mean_neg_sales_gr !=.

bysort nic_prod_code year: egen mean_sales = mean(sa_sales)
bysort nic_prod_code year: egen mean_pat = mean(sa_pat)
bysort nic_prod_code year: egen mean_pbt = mean(sa_pbt)
bysort nic_prod_code year: egen mean_pbit = mean(sa_pbit)
bysort nic_prod_code year: egen mean_pbdita = mean(sa_pbdita)

bysort nic_prod_code year: egen total_sales = sum(sa_sales)

bysort nic_prod_code year: egen total_firm_sales_growth = sum(sa_sales*firm_sales_growth)
bysort nic_prod_code year: egen total_firm_pat = sum(sa_pat*sa_sales)

gen ind_sales_growth = total_firm_sales_growth/total_sales
gen ind_pat = total_firm_pat/total_sales

duplicates drop nic_prod_code year, force

destring nic_prod_code, replace force
tsset nic_prod_code year, yearly

gen sales_growth = (mean_sales - L1.mean_sales)/L1.mean_sales

bysort year : egen sales_growth_100th = xtile(sales_growth ), n(100)

gen sales_growth_25perc = .
replace sales_growth_25perc = 1 if sales_growth_100th < 25 & sales_growth_100th != .
replace sales_growth_25perc = 0 if sales_growth_100th >= 25 & sales_growth_100th != .

gen sales_growth_50perc = .
replace sales_growth_50perc = 1 if sales_growth_100th < 50 & sales_growth_100th != .
replace sales_growth_50perc = 0 if sales_growth_100th >= 50 & sales_growth_100th != .



bysort year : egen pat_100th = xtile(mean_pat), n(100)

gen pat_25perc = .
replace pat_25perc = 1 if pat_100th < 25 & pat_100th != .
replace pat_25perc = 0 if pat_100th >= 25 & pat_100th != .

gen pat_50perc = .
replace pat_50perc = 1 if pat_100th < 50 & pat_100th != .
replace pat_50perc = 0 if pat_100th >= 50 & pat_100th != .


bysort year : egen pbt_100th = xtile(mean_pbt), n(100)

gen pbt_25perc = .
replace pbt_25perc = 1 if pbt_100th < 25 & pbt_100th != .
replace pbt_25perc = 0 if pbt_100th >= 25 & pbt_100th != .

gen pbt_50perc = .
replace pbt_50perc = 1 if pbt_100th < 50 & pbt_100th != .
replace pbt_50perc = 0 if pbt_100th >= 50 & pbt_100th != .


bysort year : egen pbit_100th = xtile(mean_pbit), n(100)

gen pbit_25perc = .
replace pbit_25perc = 1 if pbit_100th < 25 & pbit_100th != .
replace pbit_25perc = 0 if pbit_100th >= 25 & pbit_100th != .

gen pbit_50perc = .
replace pbit_50perc = 1 if pbit_100th < 50 & pbit_100th != .
replace pbit_50perc = 0 if pbit_100th >= 50 & pbit_100th != .



bysort year : egen pbdita_100th = xtile(mean_pbdita), n(100)

gen pbdita_25perc = .
replace pbdita_25perc = 1 if pbdita_100th < 25 & pbdita_100th != .
replace pbdita_25perc = 0 if pbdita_100th >= 25 & pbdita_100th != .

gen pbdita_50perc = .
replace pbdita_50perc = 1 if pbdita_100th < 50 & pbdita_100th != .
replace pbdita_50perc = 0 if pbdita_100th >= 50 & pbdita_100th != .



gen sales_growth_negative = .
replace sales_growth_negative = 1 if sales_growth<0 & sales_growth != .
replace sales_growth_negative = 0 if sales_growth>=0 & sales_growth != .

gen ind_sales_growth_negative = .
replace ind_sales_growth_negative = 1 if ind_sales_growth<0 & ind_sales_growth != .
replace ind_sales_growth_negative = 0 if ind_sales_growth>=0 & ind_sales_growth != .

gen pat_negative = .
replace pat_negative = 1 if mean_pat<0 & mean_pat != .
replace pat_negative = 0 if mean_pat>=0 & mean_pat != .

gen pbt_negative = .
replace pbt_negative = 1 if mean_pbt<0 & mean_pbt != .
replace pbt_negative = 0 if mean_pbt>=0 & mean_pbt != .

gen pbit_negative = .
replace pbit_negative = 1 if mean_pbit<0 & mean_pbit != .
replace pbit_negative = 0 if mean_pbit>=0 & mean_pbit != .

gen pbdita_negative = .
replace pbdita_negative = 1 if mean_pbdita<0 & mean_pbdita != .
replace pbdita_negative = 0 if mean_pbdita>=0 & mean_pbdita != .

gen salesgro_pat_negative = .
replace salesgro_pat_negative = 1 if mean_pat<0 & mean_pat != . & sales_growth<0 & sales_growth != .
replace salesgro_pat_negative = 0 if (mean_pat>=0 | sales_growth>=0) & mean_pat != . & sales_growth != .


keep nic_prod_code year sales_growth_25perc sales_growth_50perc sales_growth_negative pat_negative ind_neg_pat_firm_med ind_neg_sales_firm_med ind_neg_patsales_firm_med salesgro_pat_negative ind_neg_pbt_firm_med ind_sales_growth_negative ind_neg_pbit_firm_med ind_neg_pbdita_firm_med pbt_negative pbit_negative pbdita_negative pat_25perc pbt_25perc pbit_25perc pbdita_25perc pat_50perc pbt_50perc pbit_50perc pbdita_50perc

save "dta/industries_in_crisis1.dta", replace

********************************************************* Data Creation - Table 4 (Industry Crisis) (End) **********************************************



****************************************************************************************************************************************************
/* Table 4 - Industry crisis results */
****************************************************************************************************************************************************

use "dta/MCA_data_psb_year_quarter_final_industry_crisis.dta", replace

merge m:1 sa_finance1_cocode creditorcode using "dta/prev_yrs_loan_firms_quarterly.dta"

keep if _m==3
drop _m

merge m:1 creditorcode using "dta/ceo_change_quarterly.dta"

replace period_ceo = 10000 if _m==1

drop if _m==2 
drop _m

gen treat1 = 0
replace treat1 = 1 if period_new >= period_ceo

gen treat2 = 0
replace treat2 = 1 if period_new > period_ceo

bysort creditorcode: egen sum_treat1 = sum(treat1)

count if period_new >= 28 & period_new <= 33

merge m:1 sa_finance1_cocode using "dta/firms_interest_coverage.dta"
keep if _m==3
drop _m

merge m:1 sa_finance1_cocode using "dta/industry.dta"
drop if _m==2
drop _m

gen int_treat1_risky1 = treat1*risky_firm1

gen total_loan_given_dummy = 0
replace total_loan_given_dummy = 1 if total_loan_given > 0

egen firm_year = group(year sa_finance1_cocode)
egen firm_period = group(period_new  sa_finance1_cocode)
gen nic_prod_code_2 = substr(nic_prod_code,1,2)

// Bank*Firm groups
egen bank_firm = group(creditorcode sa_finance1_cocode)

// Bank*Time groups
egen bank_year = group(creditorcode year)

// Bank*Time groups
egen bank_quarter = group(creditorcode period_new)

gen firmCode = sa_finance1_cocode
gen creditorCode = creditorcode
merge m:1 firmCode creditorCode year using "dta/FirmBankYearControls_2018 - Copy.dta"
drop if _m == 2
drop _m

merge m:1 sa_finance1_cocode year using "dta/firm_year_controls.dta"
drop if _m == 2
drop _m

gen lag_amount = exp(lag_lg_amount)
gen lag_amount_assets = lag_amount/(sa_total_assets*1000000)

merge m:1 creditorcode sa_finance1_cocode year quarter using "dta/restructuring_psb_final_extended_industry_crisis.dta"

replace num_of_modifications = 0 if _m==1
replace num_of_modifications2 = 0 if _m==1

drop if _m==2
drop _m

gen num_of_modifications_dummy = 0
replace num_of_modifications_dummy = 1 if num_of_modifications > 0

gen num_of_modifications_dummy2 = 0
replace num_of_modifications_dummy2 = 1 if num_of_modifications2 > 0

destring nic_prod_code, replace force

merge m:1 nic_prod_code year using "dta/industries_in_crisis1.dta"
drop if _m == 2
drop _m

// drop if inlist(creditorcode, 236326, 236327, 236333, 236335, 236336, 236337, 236339)

// Industries in crisis dummy
gen crisis = 0
replace crisis = 1 if ind_neg_pat_firm_med == 1

// replace crisis = 1 if period_new > 33 & ind_neg_pat_firm_med == 1
// replace crisis = 1 if period_new >= 28 & period_new<=33

// replace crisis = 1 if period_new > 33 & ind_sales_growth_negative == 1
// replace crisis = 1 if period_new > 33 & pbit_negative == 1

gen int_treat1_risky1_crisis = treat1*risky_firm1*crisis
gen int_treat1_crisis = treat1*crisis
gen int_risky1_crisis = risky_firm1*crisis

reghdfe total_loan_given_dummy   int_treat1_risky1_crisis int_treat1_risky1  treat1 risky_firm1 int_treat1_crisis int_risky1_crisis crisis avg_loan_duration loan_completed_5 lag_amount_assets if period_new >= 28 & period_new <= 56, absorb(bank_quarter   bank_firm  firm_year) cluster(nic_prod_code)
outreg2 using "Referee/tables/table6_triple_interaction.xls", append label ctitle (Loan Dummy) dec(4) se coefastr bracket nocons addtext (Controls, Yes, Bank-Quarter Fixed Effects, Yes, Firm x Year Fixed Effects, Yes, Bank-Firm Fixed Effects, Yes)

reghdfe num_of_modifications_dummy   int_treat1_risky1_crisis int_treat1_risky1  treat1 risky_firm1 int_treat1_crisis int_risky1_crisis crisis avg_loan_duration loan_completed_5 lag_amount_assets if period_new >= 28 & period_new <= 56, absorb(bank_quarter   bank_firm  firm_year) cluster(nic_prod_code)
outreg2 using "Referee/tables/table6_triple_interaction.xls", append label ctitle (Restructuring) dec(4) se coefastr bracket nocons addtext (Controls, Yes, Bank-Quarter Fixed Effects, Yes, Firm x Year Fixed Effects, Yes, Bank-Firm Fixed Effects, Yes)



********************************************************* Data Creation - Table 5 (AQR) (Begin) *****************************************************

use "dta/ceo_change_quarterly.dta", replace
drop if creditorcode == 236337
save "dta/ceo_change_quarterly_new1.dta", replace

import delimited "raw\divergence_banks.txt", delimiter("|") clear 
destring sa_total_income - sa_bd_divergence_provisioning , force replace

tostring sa_finance1_year, force replace
gen day = substr(sa_finance1_year,7,2)
gen month = substr(sa_finance1_year,5,2)
gen year = substr(sa_finance1_year,1,4)

destring day-year, force replace

duplicates tag sa_finance1_cocode year, gen(x)
bysort sa_finance1_cocode: gen march = 1 if x>0 & month == 3
bysort sa_finance1_cocode: egen max_march = max(march) if x>0
drop if x>0 & max_march == 1 & march != 1
sort sa_finance1_cocode year month
bysort sa_finance1_cocode year: gen dup_n = _n if x > 0 & max_march != 1 
drop if dup_n > 1 & dup_n != .
sort sa_finance1_cocode year month day
bysort sa_finance1_cocode year: egen march_dup = sum(march) if x > 0 & max_march == 1 
bysort sa_finance1_cocode year: gen march_dup_n = _n if march_dup > 1 & march_dup != .
bysort sa_finance1_cocode year: gen march_dup_total_n = _N if march_dup > 1 & march_dup != .
drop if march_dup_n < march_dup_total_n & march_dup_n != .

drop x-march_dup_total_n

rename sa_finance1_cocode creditorcode

merge m:1 creditorcode using "dta/ceo_change_quarterly_new1.dta"
drop _m

merge m:1 creditorcode using "dta/public_sector_banks.dta"
keep if _m==3
drop _m

gen quarter = 1 if month >3 & month <=6
replace quarter = 1 if month >6 & month <=9
replace quarter = 1 if month >9 & month <=12
replace quarter = 1 if month >=1 & month <=3

save "dta/divergence_banks_revised_oldCEO.dta", replace

********************************************************* Data Creation - Table 5 (AQR) (End) ******************************************************



****************************************************************************************************************************************************
/* Table 5 - AQR */
****************************************************************************************************************************************************

use "dta/divergence_banks_revised_oldCEO.dta", replace

// Non-public sector central banks
drop if creditorcode == 496827 | creditorcode == 100283

tsset creditorcode year, yearly
tsfill, full

bysort creditorcode (period_ceo): replace period_ceo=period_ceo[_n-1] if missing(period_ceo)
gen treat = 0
replace treat = 1 if period_ceo >= 28 & period_ceo <= 33

merge m:1 creditorcode year using "dta/capital_adequacy_ratio.dta"
drop if _m==2
drop _m

rename creditorcode sa_finance1_cocode

merge m:1 sa_finance1_cocode year using "dta/alternate_risk_criteria_data_interim1.dta"
drop if _m==2
drop _m

// Divergenece variables
gen gross_div_ratio = sa_bd_divergence_gross_npa/ sa_bd_gross_npa_bank
gen npa_div_ratio = sa_bd_divergence_net_npa / sa_bd_net_npa_bank
gen prov_div_ratio = sa_bd_divergence_provisioning / sa_bd_provisioning_npa_bank

gen log_div_prov = log(1 + sa_bd_divergence_provisioning)

gen gross_inc_ratio = sa_bd_divergence_gross_npa/ sa_total_income
gen npa_inc_ratio = sa_bd_divergence_net_npa / sa_total_income
gen prov_inc_ratio = sa_bd_divergence_provisioning / sa_total_income
gen gross_pat_ratio = sa_bd_divergence_gross_npa/ sa_pat
gen npa_pat_ratio = sa_bd_divergence_net_npa / sa_pat
gen prov_pat_ratio = sa_bd_divergence_provisioning / sa_pat
gen gross_loan_ratio = sa_bd_divergence_gross_npa/ sa_total_loans_and_advances
gen npa_loan_ratio = sa_bd_divergence_net_npa / sa_total_loans_and_advances
gen prov_loan_ratio = sa_bd_divergence_provisioning / (sa_total_loans_and_advances)
gen gross_assets_ratio = sa_bd_divergence_gross_npa/ sa_total_assets
gen npa_assets_ratio = sa_bd_divergence_net_npa / sa_total_assets
gen prov_assets_ratio = sa_bd_divergence_provisioning / sa_total_assets
gen prov_gnpa_ratio = sa_bd_divergence_provisioning / (sa_bd_gross_npa_closing_bal)

gen log_income_year = log(1 + sa_total_income)
gen pat_income = sa_pat/sa_total_income
gen gnpa_close_loan_adv = sa_bd_gross_npa_closing_bal/sa_total_loans_and_advances

gen pat_income_perc = pat_income*100
gen gnpa_close_loan_adv_perc = gnpa_close_loan_adv*100

eststo clear
label variable treat "Banks With CEO Turonver"

gen prov_inc_ratio_perc = 100*prov_inc_ratio

summ treat log_income_year pat_income_perc gnpa_close_loan_adv_perc prov_inc_ratio_perc if year >= 2017 & year <= 2020, d

// reghdfe prov_inc_ratio_perc treat log_income_year pat_income gnpa_close_loan_adv if year >= 2017 & year <= 2020, noabsorb
// outreg2 using "Referee/tables/table8_AQR.xls", replace label ctitle (Divergence in provisioning/Income) dec(4) se coefastr bracket nocons addtext (Controls, Yes, Year Fixed Effects, No)
reghdfe prov_inc_ratio_perc treat log_income_year pat_income_perc gnpa_close_loan_adv_perc if year >= 2017 & year <= 2020, absorb(year)
outreg2 using "Referee/tables/table8_AQR.xls", replace label ctitle (Divergence in provisioning/Income) dec(4) se coefastr bracket nocons addtext (Controls, Yes, Year Fixed Effects, Yes)

foreach var in prov_inc_ratio prov_assets_ratio prov_gnpa_ratio log_div_prov prov_inc_ratio_perc {
    replace `var' = 0 if `var' == .
}

// reghdfe prov_inc_ratio_perc treat log_income_year pat_income gnpa_close_loan_adv if year >= 2017 & year <= 2020, noabsorb
// outreg2 using "Referee/tables/table8_AQR.xls", append label ctitle (Divergence in provisioning/Income) dec(4) se coefastr bracket nocons addtext (Controls, Yes, Year Fixed Effects, No)
reghdfe prov_inc_ratio_perc treat log_income_year pat_income_perc gnpa_close_loan_adv_perc if year >= 2017 & year <= 2020, absorb(year)
outreg2 using "Referee/tables/table8_AQR.xls", append label ctitle (Divergence in provisioning/Income) dec(4) se coefastr bracket nocons addtext (Controls, Yes, Year Fixed Effects, Yes)



********************************************************* Data Creation - Table 6 (Col. 1 & 2) (Firm Performance) (Begin) *****************************

****************************************************************************************************************************************************
/* Liabilities */
****************************************************************************************************************************************************

import delimited "raw\liabilities.txt", delimiter("|") clear

destring sa_total_liabilities-sa_debt_equity, force replace

tostring sa_finance1_year, force replace
gen day = substr(sa_finance1_year,7,2)
gen month = substr(sa_finance1_year,5,2)
gen year = substr(sa_finance1_year,1,4)

destring day-year, force replace

duplicates tag sa_finance1_cocode year, gen(x)
bysort sa_finance1_cocode: gen march = 1 if x>0 & month == 3
bysort sa_finance1_cocode: egen max_march = max(march) if x>0
drop if x>0 & max_march == 1 & march != 1
sort sa_finance1_cocode year month
bysort sa_finance1_cocode year: gen dup_n = _n if x > 0 & max_march != 1 
drop if dup_n > 1 & dup_n != .
sort sa_finance1_cocode year month day
bysort sa_finance1_cocode year: egen march_dup = sum(march) if x > 0 & max_march == 1 
bysort sa_finance1_cocode year: gen march_dup_n = _n if march_dup > 1 & march_dup != .
bysort sa_finance1_cocode year: gen march_dup_total_n = _N if march_dup > 1 & march_dup != .
drop if march_dup_n < march_dup_total_n & march_dup_n != .

drop x-march_dup_total_n

save "dta/liabilities.dta", replace

****************************************************************************************************************************************************
/* Investments Table 9 */
****************************************************************************************************************************************************

import delimited "raw\investments_table9.txt", varnames(1) clear

destring sa_total_income-sa_plant_addn, force replace

tostring sa_finance1_year, force replace
gen day = substr(sa_finance1_year,7,2)
gen month = substr(sa_finance1_year,5,2)
gen year = substr(sa_finance1_year,1,4)

destring day-year, force replace

duplicates tag sa_finance1_cocode year, gen(x)
bysort sa_finance1_cocode: gen march = 1 if x>0 & month == 3
bysort sa_finance1_cocode: egen max_march = max(march) if x>0
drop if x>0 & max_march == 1 & march != 1
sort sa_finance1_cocode year month
bysort sa_finance1_cocode year: gen dup_n = _n if x > 0 & max_march != 1 
drop if dup_n > 1 & dup_n != .
sort sa_finance1_cocode year month day
bysort sa_finance1_cocode year: egen march_dup = sum(march) if x > 0 & max_march == 1 
bysort sa_finance1_cocode year: gen march_dup_n = _n if march_dup > 1 & march_dup != .
bysort sa_finance1_cocode year: gen march_dup_total_n = _N if march_dup > 1 & march_dup != .
drop if march_dup_n < march_dup_total_n & march_dup_n != .

drop x-march_dup_total_n

tsset sa_finance1_cocode year, yearly

gen per_chg_ppe_f1 = 100*(f1.sa_net_ppe - sa_net_ppe)/sa_net_ppe

gen per_chg_grossppe_f1 = 100*(f1.sa_gross_property_plant_and_equi - sa_gross_property_plant_and_equi)/sa_gross_property_plant_and_equi
gen per_chg_grossppe_f2 = 100*(f2.sa_gross_property_plant_and_equi - sa_gross_property_plant_and_equi)/sa_gross_property_plant_and_equi
gen per_chg_grossppe_f3 = 100*(f3.sa_gross_property_plant_and_equi - sa_gross_property_plant_and_equi)/sa_gross_property_plant_and_equi
gen per_chg_grossppe_f4 = 100*(f4.sa_gross_property_plant_and_equi - sa_gross_property_plant_and_equi)/sa_gross_property_plant_and_equi
gen per_chg_grossppe_f5 = 100*(f5.sa_gross_property_plant_and_equi - sa_gross_property_plant_and_equi)/sa_gross_property_plant_and_equi

gen per_chg_pmc_f1 = 100*(f1.sa_net_plant_mach_computer_elec - sa_net_plant_mach_computer_elec)/sa_net_plant_mach_computer_elec

gen per_chg_assets_f1 = 100*(f1.sa_total_assets - sa_total_assets)/sa_total_assets

gen per_chg_plant_f1 = 100*(f1.sa_net_plant - sa_net_plant)/sa_net_plant

gen per_chg_income_f1 = 100*(f1.sa_total_income - sa_total_income)/sa_total_income

gen per_chg_sales_f1 = 100*(f1.sa_sales - sa_sales)/sa_sales

gen per_chg_pat_f1 = 100*(f1.sa_pat - sa_pat)/sa_pat

gen per_chg_pbt_f1 = 100*(f1.sa_pbt - sa_pbt)/sa_pbt

gen per_chg_pbit_f1 = 100*(f1.sa_pbit - sa_pbit)/sa_pbit

gen per_chg_pbpt_f1 = 100*(f1.sa_pbpt - sa_pbpt)/sa_pbpt

gen per_chg_pbdita_f1 = 100*(f1.sa_pbdita - sa_pbdita)/sa_pbdita

gen per_chg_assets = 100*(sa_total_assets - L1.sa_total_assets)/L1.sa_total_assets

gen per_chg_ppe = 100*(sa_net_ppe - L1.sa_net_ppe)/L1.sa_net_ppe

gen per_chg_grossppe = 100*(sa_gross_property_plant_and_equi - L1.sa_gross_property_plant_and_equi)/L1.sa_gross_property_plant_and_equi

gen per_chg_pmc = 100*(sa_net_plant_mach_computer_elec - L1.sa_net_plant_mach_computer_elec)/L1.sa_net_plant_mach_computer_elec

gen per_chg_plant = 100*(sa_net_plant - L1.sa_net_plant)/L1.sa_net_plant

gen per_chg_pat = 100*(sa_pat - L1.sa_pat)/L1.sa_pat

gen per_chg_pbt = 100*(sa_pbt - L1.sa_pbt)/L1.sa_pbt

gen per_chg_pbit = 100*(sa_pbit - L1.sa_pbit)/L1.sa_pbit

gen per_chg_pbpt = 100*(sa_pbpt - L1.sa_pbpt)/L1.sa_pbpt

gen per_chg_pbdita = 100*(sa_pbdita - L1.sa_pbdita)/L1.sa_pbdita

gen ppe_add_grossppe = sa_total_additions_to_ppe_in_yea/sa_gross_property_plant_and_equi

gen ppe_add_asset = sa_total_additions_to_ppe_in_yea/sa_total_assets

gen ppe_add_grossppe_f1 = (f1.sa_total_additions_to_ppe_in_yea)/sa_gross_property_plant_and_equi

gen ppe_add_asset_f1 = (f1.sa_total_additions_to_ppe_in_yea)/sa_total_assets

save "dta/investments_table9.dta", replace

****************************************************************************************************************************************************
/* Investments Bottom quartile Table */
****************************************************************************************************************************************************

use "dta/MCA_data_psb_year_quarter_final.dta", replace

merge m:1 sa_finance1_cocode creditorcode using "dta/prev_yrs_loan_firms_quarterly.dta"

keep if _m==3
drop _m

merge m:1 creditorcode using "dta/ceo_change_quarterly.dta"

replace period_ceo = 10000 if _m==1

drop if _m==2 
drop _m

gen treat1 = 0
replace treat1 = 1 if period_new >= period_ceo

bysort creditorcode: egen max_treat1 = max(treat1)

merge m:1 sa_finance1_cocode creditorcode year quarter using "dta/sanctioned_loans_outstanding.dta"

replace loan_amount_sanctioned = 0 if _m==1

drop if _m==2
drop _m

merge m:1 sa_finance1_cocode creditorcode year quarter using "dta/repaid_loans_outstanding.dta"

replace loan_amount_repayment = 0 if _m==1

drop if _m==2
drop _m

egen firm_bank = group(sa_finance1_cocode creditorcode)

tsset firm_bank period_new, quarterly

gen outstanding_sanctioned = 0
replace outstanding_sanctioned = loan_amount_sanctioned if L1.outstanding_sanctioned == .
replace outstanding_sanctioned = loan_amount_sanctioned + L1.outstanding_sanctioned if L1.outstanding_sanctioned != .

gen outstanding_repayment = 0
replace outstanding_repayment = loan_amount_repayment if L1.outstanding_repayment == .
replace outstanding_repayment = loan_amount_repayment + L1.outstanding_repayment if L1.outstanding_repayment != .

gen oustanding_amount = outstanding_sanctioned - outstanding_repayment

keep if ((year == 2008) | (year == 2009) | (year == 2010)) & (quarter == 4)

bysort sa_finance1_cocode year: egen total_oustanding_amount = sum(oustanding_amount)

duplicates drop sa_finance1_cocode year, force

keep sa_finance1_cocode year total_oustanding_amount

save "dta/outstanding_amount_2008_10.dta", replace

use "dta/tables.dta", replace

keep if period_new >= 28 & period_new <= 33

bysort sa_finance1_cocode year: egen total_loan_yearly = sum(total_loan_amount)

bysort sa_finance1_cocode year treat1: egen total_loan_yearly_treat = sum(total_loan_amount)

gen ratio_loan_yearly_treat = total_loan_yearly_treat/total_loan_yearly

duplicates drop sa_finance1_cocode year treat1, force

keep if treat1 == 1

drop if total_loan_yearly_treat == 0

duplicates drop sa_finance1_cocode year, force

merge m:1 sa_finance1_cocode year using "dta/outstanding_amount_2008_10.dta"
keep if _m==3
drop _m

gen loan_give_outst_ratio = 100*(total_loan_yearly_treat/total_oustanding_amount)

gen lg_total_loan_yearly = log(1 + total_loan_yearly_treat)

keep sa_finance1_cocode year lg_total_loan_yearly ratio_loan_yearly_treat total_loan_yearly_treat loan_give_outst_ratio total_oustanding_amount

save "dta/exposure_ceo_changed_bank_yearly.dta", replace

use "dta/tables.dta", replace

keep if period_new >= 28 & period_new <= 33

bysort sa_finance1_cocode year: egen total_loan_given_dummy_year = sum(total_loan_given_dummy)
bysort sa_finance1_cocode year: egen total_loan_given_year = sum(loanAmount)

duplicates drop sa_finance1_cocode year, force

keep sa_finance1_cocode year total_loan_given_dummy_year total_loan_given_year

save "dta/loan_given_year.dta", replace

use "dta/tables.dta", replace

keep if period_new >= 28 & period_new <= 33

bysort sa_finance1_cocode year: egen total_loan_yearly = sum(total_loan_amount)

bysort sa_finance1_cocode year treat1: egen total_loan_yearly_treat = sum(total_loan_amount)

gen ratio_loan_yearly_treat = total_loan_yearly_treat/total_loan_yearly

replace ratio_loan_yearly_treat = (1 - ratio_loan_yearly_treat) if treat1 == 0

gen perc_loan_treat_psb_bank = 100*ratio_loan_yearly_treat

rename total_loan_yearly_treat total_loan_given_treat

keep sa_finance1_cocode year perc_loan_treat_psb_bank total_loan_given_treat

duplicates drop sa_finance1_cocode year, force 

save "dta/perc_loan_treat_psb_bank.dta", replace

use "dta/MCA_data_with_dates.dta", replace

keep if sanctionDate >= td(01apr2001) & sanctionDate <= td(31mar2010)

rename (creditorCode firmCode) (creditorcode firmcode)

gen period = .

replace period = 1 if sanctionDate >= td(01apr2001) & sanctionDate <= td(30jun2001)
replace period = 2 if sanctionDate >= td(01jul2001) & sanctionDate <= td(30sep2001)
replace period = 3 if sanctionDate >= td(01oct2001) & sanctionDate <= td(31dec2001)
replace period = 4 if sanctionDate >= td(01jan2002) & sanctionDate <= td(31mar2002)
replace period = 5 if sanctionDate >= td(01apr2002) & sanctionDate <= td(30jun2002)
replace period = 6 if sanctionDate >= td(01jul2002) & sanctionDate <= td(30sep2002)
replace period = 7 if sanctionDate >= td(01oct2002) & sanctionDate <= td(31dec2002)
replace period = 8 if sanctionDate >= td(01jan2003) & sanctionDate <= td(31mar2003)
replace period = 9 if sanctionDate >= td(01apr2003) & sanctionDate <= td(30jun2003)
replace period = 10 if sanctionDate >= td(01jul2003) & sanctionDate <= td(30sep2003)
replace period = 11 if sanctionDate >= td(01oct2003) & sanctionDate <= td(31dec2003)
replace period = 12 if sanctionDate >= td(01jan2004) & sanctionDate <= td(31mar2004)
replace period = 13 if sanctionDate >= td(01apr2004) & sanctionDate <= td(30jun2004)
replace period = 14 if sanctionDate >= td(01jul2004) & sanctionDate <= td(30sep2004)
replace period = 15 if sanctionDate >= td(01oct2004) & sanctionDate <= td(31dec2004)
replace period = 16 if sanctionDate >= td(01jan2005) & sanctionDate <= td(31mar2005)
replace period = 17 if sanctionDate >= td(01apr2005) & sanctionDate <= td(30jun2005)
replace period = 18 if sanctionDate >= td(01jul2005) & sanctionDate <= td(30sep2005)
replace period = 19 if sanctionDate >= td(01oct2005) & sanctionDate <= td(31dec2005)
replace period = 20 if sanctionDate >= td(01jan2006) & sanctionDate <= td(31mar2006)
replace period = 21 if sanctionDate >= td(01apr2006) & sanctionDate <= td(30jun2006)
replace period = 22 if sanctionDate >= td(01jul2006) & sanctionDate <= td(30sep2006)
replace period = 23 if sanctionDate >= td(01oct2006) & sanctionDate <= td(31dec2006)
replace period = 24 if sanctionDate >= td(01jan2007) & sanctionDate <= td(31mar2007)
replace period = 25 if sanctionDate >= td(01apr2007) & sanctionDate <= td(30jun2007)
replace period = 26 if sanctionDate >= td(01jul2007) & sanctionDate <= td(30sep2007)
replace period = 27 if sanctionDate >= td(01oct2007) & sanctionDate <= td(31dec2007)
replace period = 28 if sanctionDate >= td(01jan2008) & sanctionDate <= td(31mar2008)
replace period = 29 if sanctionDate >= td(01apr2008) & sanctionDate <= td(30jun2008)
replace period = 30 if sanctionDate >= td(01jul2008) & sanctionDate <= td(30sep2008)
replace period = 31 if sanctionDate >= td(01oct2008) & sanctionDate <= td(31dec2008)
replace period = 32 if sanctionDate >= td(01jan2009) & sanctionDate <= td(31mar2009)
replace period = 33 if sanctionDate >= td(01apr2009) & sanctionDate <= td(30jun2009)
replace period = 34 if sanctionDate >= td(01jul2009) & sanctionDate <= td(30sep2009)
replace period = 35 if sanctionDate >= td(01oct2009) & sanctionDate <= td(31dec2009)
replace period = 36 if sanctionDate >= td(01jan2010) & sanctionDate <= td(31mar2010)

gen year = int((period-1)/4)
replace year = year + 2002
gen quarter = mod(period,4)
replace quarter = 4 if quarter == 0

keep if period >= 28 & period <= 33

rename firmcode sa_finance1_cocode

bysort sa_finance1_cocode year: egen total_loan_given_year_any = sum(loanAmount)

keep sa_finance1_cocode year total_loan_given_year_any

duplicates drop sa_finance1_cocode year, force

save "dta/loan_given_year_any_bank1.dta", replace

use "dta/tables.dta", replace

keep if period_new >= 28 & period_new <= 33

duplicates drop sa_finance1_cocode, force

keep sa_finance1_cocode

save "dta/sample_firms.dta", replace

********************************************************* Data Creation - Table 6 (Col. 1 & 2) (Firm Performance) (End) *******************************



****************************************************************************************************************************************************
/* Table 6 (Col. 1 & 2) - Firm Performance */
****************************************************************************************************************************************************

use "dta/investments_table9.dta", replace 

keep if year >= 2008 & year <= 2010

merge m:1 sa_finance1_cocode year using "dta/liabilities.dta", force
drop if _m==2
drop _m

// merge m:1 sa_finance1_cocode year using "dta_hrishikesh/loans_of_firms_treat_wise_yearly_revised_oldCEO.dta"
// keep if _m==3
// drop _m

merge m:1 sa_finance1_cocode year using "dta/exposure_ceo_changed_bank_yearly.dta"
drop if _m==2

gen exposure_dummy = 0
replace exposure_dummy = 1 if _m==3

replace total_loan_yearly = 0 if _m==1
replace lg_total_loan_yearly = 0 if _m==1
replace ratio_loan_yearly_treat = 0 if _m==1
replace loan_give_outst_ratio = 0 if _m==1

drop _m

merge m:1 sa_finance1_cocode using "dta/industry.dta"
drop if _m==2
drop _m

egen industry_year = group(nic_prod_code year)

merge m:1 sa_finance1_cocode using "dta/firms_interest_coverage.dta"
drop if _m==2
drop _m

merge m:1 sa_finance1_cocode using "dta/sample_firms.dta", force
keep if _m==3
drop _m

merge m:1 sa_finance1_cocode year using "dta/perc_loan_treat_psb_bank.dta"
drop if _m==2
drop _m

merge m:1 sa_finance1_cocode year using "dta/loan_given_year.dta"
drop if _m==2
drop _m

merge m:1 sa_finance1_cocode year using "dta/loan_given_year_any_bank1.dta"
drop if _m==2
drop _m

gen perc_loan_treat_any = 100*(total_loan_given_treat/total_loan_given_year_any)

gen pat_income = 100*(sa_pat/sa_total_income)
gen lg_assets = log(1+sa_total_assets)
gen lg_income = log(1+sa_total_income)
gen lg_add = log(1 + sa_gross_fixed_assets_tot_addn)
gen add_assets = 100*(sa_gross_fixed_assets_tot_addn/sa_total_assets)
gen add_total_ppe_assets = 100*( sa_total_additions_to_ppe_in_yea /sa_total_assets)
gen add_total_ppe_grossppe= 100*( sa_total_additions_to_ppe_in_yea /sa_gross_property_plant_and_equi)
gen add_total_ppe_ppe= 100*( sa_total_additions_to_ppe_in_yea /sa_net_ppe)

gen total_loan_yearly_ratio = 100*(total_loan_yearly_treat/(sa_debt*1000000))

gen low_icr = .
replace low_icr = 0 if avg_int_cov_ratio >= 1 & avg_int_cov_ratio != .
replace low_icr = 1 if avg_int_cov_ratio < 1 & avg_int_cov_ratio != .

gen high_icr = .
replace high_icr = 0 if avg_int_cov_ratio <=  7.52 & avg_int_cov_ratio != .
replace high_icr = 1 if avg_int_cov_ratio >  7.52 & avg_int_cov_ratio != .

gen exp_low_duumy = low_icr*exposure_dummy
gen exp_high_dummy = high_icr*exposure_dummy

gen exp_low = low_icr*total_loan_yearly_ratio
gen exp_high = high_icr*total_loan_yearly_ratio

gen exp_low_lg = low_icr*loan_give_outst_ratio
gen exp_high_lg = high_icr*loan_give_outst_ratio

gen exp_low_psb = low_icr*perc_loan_treat_psb_bank
gen exp_high_psb = high_icr*perc_loan_treat_psb_bank

gen exp_low_any = low_icr*perc_loan_treat_any
gen exp_high_any = high_icr*perc_loan_treat_any

// drop if total_loan_yearly_ratio > 100

keep if perc_loan_treat_any != .

winsor2 per_chg_grossppe_f1 per_chg_pbdita_f1 pat_income per_chg_pbit_f1 add_assets add_total_ppe_assets per_chg_assets_f1, cuts(5 95)

winsor2 total_loan_yearly_ratio loan_give_outst_ratio perc_loan_treat_psb_bank perc_loan_treat_any, cuts(1 99)

gen exp_low_w = low_icr*total_loan_yearly_ratio_w
gen exp_high_w = high_icr*total_loan_yearly_ratio_w
gen exp_low_lg_w = low_icr*loan_give_outst_ratio_w
gen exp_high_lg_w = high_icr*loan_give_outst_ratio_w
gen exp_low_psb_w = low_icr*perc_loan_treat_psb_bank_w
gen exp_high_psb_w = high_icr*perc_loan_treat_psb_bank_w
gen exp_low_any_w = low_icr*perc_loan_treat_any_w
gen exp_high_any_w = high_icr*perc_loan_treat_any_w

// egen int_cover_quartile = xtile(avg_int_cov_ratio), n(4) 
//
// gen bottom_quartile = 0
// replace bottom_quartile = 1 if int_cover_quartile == 1
//
// gen top_quartile = 0
// replace top_quartile = 1 if int_cover_quartile == 4
//
// gen treat_bottom = bottom_quartile*perc_loan_treat_bankmean
// gen treat_top = top_quartile*perc_loan_treat_bankmean

eststo clear
label variable total_loan_yearly_ratio "Exposure to CEO changed bank"
label variable exp_low "Low-quality firm x Exposure to CEO changed bank"
label variable low_icr "Low-quality firm"

summ pat_income_w lg_income lg_assets total_loan_yearly_ratio add_total_ppe_assets_w per_chg_pbit_f1_w, d

reghdfe lg_add  exp_low_any low_icr perc_loan_treat_any  pat_income_w lg_income lg_assets, absorb(sa_finance1_cocode year) cluster(nic_prod_code)
outreg2 using "Referee/tables/table_PPE.xls", replace label ctitle (Log(Total Additions to Gross Fixed Assets)) dec(4) se coefastr bracket nocons addtext (Controls, Yes, Firm Fixed Effects, Yes, Year Fixed Effects, Yes)

reghdfe per_chg_pbdita_f1_w   exp_low_any low_icr perc_loan_treat_any  pat_income_w lg_income lg_assets, absorb(sa_finance1_cocode year) cluster(nic_prod_code)
outreg2 using "Referee/tables/table_PPE.xls", append label ctitle (PBDITA) dec(4) se coefastr bracket nocons addtext (Controls, Yes, Firm Fixed Effects, Yes, Year Fixed Effects, Yes)



********************************************************* Data Creation - Table 6 (Col. 3 & 4) (Firm Performance) (Begin) *****************************

****************************************************************************************************************************************************
// Pre-Crisis
****************************************************************************************************************************************************

use "dta/tables_pre_crisis_trimmed.dta", replace

duplicates drop sa_finance1_cocode, force

keep sa_finance1_cocode

save "dta/sample_firms_pre_crisis.dta", replace

use "dta/tables_pre_crisis_trimmed.dta", replace

keep if period_new >= 16 & period_new <= 27

bysort sa_finance1_cocode year: egen total_loan_yearly = sum(total_loan_amount)

bysort sa_finance1_cocode year treat1: egen total_loan_yearly_treat = sum(total_loan_amount)

gen ratio_loan_yearly_treat = total_loan_yearly_treat/total_loan_yearly

duplicates drop sa_finance1_cocode year treat1, force

keep if treat1 == 1

drop if total_loan_yearly_treat == 0

duplicates drop sa_finance1_cocode year, force

gen lg_total_loan_yearly = log(1 + total_loan_yearly_treat)

keep sa_finance1_cocode year lg_total_loan_yearly ratio_loan_yearly_treat total_loan_yearly_treat

save "dta/exposure_ceo_changed_bank_yearly_pre_crisis.dta", replace

use "dta/tables.dta", replace

keep if period_new >= 16 & period_new <= 27

bysort sa_finance1_cocode year: egen total_loan_yearly = sum(total_loan_amount)

bysort sa_finance1_cocode year treat1: egen total_loan_yearly_treat = sum(total_loan_amount)

gen ratio_loan_yearly_treat = total_loan_yearly_treat/total_loan_yearly

replace ratio_loan_yearly_treat = (1 - ratio_loan_yearly_treat) if treat1 == 0

gen perc_loan_treat_psb_bank = 100*ratio_loan_yearly_treat

rename total_loan_yearly_treat total_loan_given_treat

keep sa_finance1_cocode year perc_loan_treat_psb_bank total_loan_given_treat

duplicates drop sa_finance1_cocode year, force 

save "dta/perc_loan_treat_psb_bank_pre_crisis.dta", replace

use "dta/MCA_data_with_dates.dta", replace

keep if sanctionDate >= td(01apr2001) & sanctionDate <= td(31mar2010)

rename (creditorCode firmCode) (creditorcode firmcode)

gen period = .

replace period = 1 if sanctionDate >= td(01apr2001) & sanctionDate <= td(30jun2001)
replace period = 2 if sanctionDate >= td(01jul2001) & sanctionDate <= td(30sep2001)
replace period = 3 if sanctionDate >= td(01oct2001) & sanctionDate <= td(31dec2001)
replace period = 4 if sanctionDate >= td(01jan2002) & sanctionDate <= td(31mar2002)
replace period = 5 if sanctionDate >= td(01apr2002) & sanctionDate <= td(30jun2002)
replace period = 6 if sanctionDate >= td(01jul2002) & sanctionDate <= td(30sep2002)
replace period = 7 if sanctionDate >= td(01oct2002) & sanctionDate <= td(31dec2002)
replace period = 8 if sanctionDate >= td(01jan2003) & sanctionDate <= td(31mar2003)
replace period = 9 if sanctionDate >= td(01apr2003) & sanctionDate <= td(30jun2003)
replace period = 10 if sanctionDate >= td(01jul2003) & sanctionDate <= td(30sep2003)
replace period = 11 if sanctionDate >= td(01oct2003) & sanctionDate <= td(31dec2003)
replace period = 12 if sanctionDate >= td(01jan2004) & sanctionDate <= td(31mar2004)
replace period = 13 if sanctionDate >= td(01apr2004) & sanctionDate <= td(30jun2004)
replace period = 14 if sanctionDate >= td(01jul2004) & sanctionDate <= td(30sep2004)
replace period = 15 if sanctionDate >= td(01oct2004) & sanctionDate <= td(31dec2004)
replace period = 16 if sanctionDate >= td(01jan2005) & sanctionDate <= td(31mar2005)
replace period = 17 if sanctionDate >= td(01apr2005) & sanctionDate <= td(30jun2005)
replace period = 18 if sanctionDate >= td(01jul2005) & sanctionDate <= td(30sep2005)
replace period = 19 if sanctionDate >= td(01oct2005) & sanctionDate <= td(31dec2005)
replace period = 20 if sanctionDate >= td(01jan2006) & sanctionDate <= td(31mar2006)
replace period = 21 if sanctionDate >= td(01apr2006) & sanctionDate <= td(30jun2006)
replace period = 22 if sanctionDate >= td(01jul2006) & sanctionDate <= td(30sep2006)
replace period = 23 if sanctionDate >= td(01oct2006) & sanctionDate <= td(31dec2006)
replace period = 24 if sanctionDate >= td(01jan2007) & sanctionDate <= td(31mar2007)
replace period = 25 if sanctionDate >= td(01apr2007) & sanctionDate <= td(30jun2007)
replace period = 26 if sanctionDate >= td(01jul2007) & sanctionDate <= td(30sep2007)
replace period = 27 if sanctionDate >= td(01oct2007) & sanctionDate <= td(31dec2007)
replace period = 28 if sanctionDate >= td(01jan2008) & sanctionDate <= td(31mar2008)
replace period = 29 if sanctionDate >= td(01apr2008) & sanctionDate <= td(30jun2008)
replace period = 30 if sanctionDate >= td(01jul2008) & sanctionDate <= td(30sep2008)
replace period = 31 if sanctionDate >= td(01oct2008) & sanctionDate <= td(31dec2008)
replace period = 32 if sanctionDate >= td(01jan2009) & sanctionDate <= td(31mar2009)
replace period = 33 if sanctionDate >= td(01apr2009) & sanctionDate <= td(30jun2009)
replace period = 34 if sanctionDate >= td(01jul2009) & sanctionDate <= td(30sep2009)
replace period = 35 if sanctionDate >= td(01oct2009) & sanctionDate <= td(31dec2009)
replace period = 36 if sanctionDate >= td(01jan2010) & sanctionDate <= td(31mar2010)

gen year = int((period-1)/4)
replace year = year + 2002
gen quarter = mod(period,4)
replace quarter = 4 if quarter == 0

keep if period >= 16 & period <= 27

rename firmcode sa_finance1_cocode

bysort sa_finance1_cocode year: egen total_loan_given_year_any = sum(loanAmount)

keep sa_finance1_cocode year total_loan_given_year_any

duplicates drop sa_finance1_cocode year, force

save "dta/loan_given_year_any_bank_pre_crisis.dta", replace

********************************************************* Data Creation - Table 6 (Col. 3 & 4) (Firm Performance) (End) *******************************



****************************************************************************************************************************************************
/* Table 6 (Col. 3 & 4) - Firm Performance */
****************************************************************************************************************************************************

use "dta/investments_table9.dta", replace

keep if year >= 2005 & year <= 2007

merge m:1 sa_finance1_cocode year using "dta/liabilities.dta", force
drop if _m==2
drop _m

// merge m:1 sa_finance1_cocode year using "dta_hrishikesh/loans_of_firms_treat_wise_yearly_revised_oldCEO.dta"
// keep if _m==3
// drop _m

merge m:1 sa_finance1_cocode year using "dta/exposure_ceo_changed_bank_yearly_pre_crisis.dta"
drop if _m==2

gen exposure_dummy = 0
replace exposure_dummy = 1 if _m==3

replace total_loan_yearly = 0 if _m==1
replace lg_total_loan_yearly = 0 if _m==1
replace ratio_loan_yearly_treat = 0 if _m==1

drop _m

merge m:1 sa_finance1_cocode using "dta/industry.dta"
drop if _m==2
drop _m

gen pat_income = 100*(sa_pat/sa_total_income)
gen lg_assets = log(1+sa_total_assets)
gen lg_income = log(1+sa_total_income)
gen add_total_ppe_assets = 100*( sa_total_additions_to_ppe_in_yea /sa_total_assets)
gen lg_add = log(1 + sa_gross_fixed_assets_tot_addn)

egen industry_year = group(nic_prod_code year)

merge m:1 sa_finance1_cocode using "dta/firms_interest_coverage_pre_crisis.dta"
drop if _m==2
drop _m

merge m:1 sa_finance1_cocode using "dta/sample_firms_pre_crisis.dta", force
keep if _m==3
drop _m

gen total_loan_yearly_ratio = 100*(total_loan_yearly_treat/(sa_debt*1000000))

merge m:1 sa_finance1_cocode year using "dta/perc_loan_treat_psb_bank_pre_crisis.dta"
drop if _m==2
drop _m

merge m:1 sa_finance1_cocode year using "dta/loan_given_year_any_bank_pre_crisis.dta"
drop if _m==2
drop _m

gen perc_loan_treat_any = 100*(total_loan_given_treat/total_loan_given_year_any)

gen low_icr = .
replace low_icr = 0 if avg_int_cov_ratio >= 1 & avg_int_cov_ratio != .
replace low_icr = 1 if avg_int_cov_ratio < 1 & avg_int_cov_ratio != .

gen high_icr = .
replace high_icr = 0 if avg_int_cov_ratio <=  7.52 & avg_int_cov_ratio != .
replace high_icr = 1 if avg_int_cov_ratio >  7.52 & avg_int_cov_ratio != .

gen exp_low_duumy = low_icr*exposure_dummy
gen exp_high_dummy = high_icr*exposure_dummy

gen exp_low = low_icr*total_loan_yearly_ratio
gen exp_high = high_icr*total_loan_yearly_ratio

gen exp_low_any = low_icr*perc_loan_treat_any
gen exp_high_any = high_icr*perc_loan_treat_any

// drop if total_loan_yearly_ratio > 100

keep if perc_loan_treat_any != .

winsor2 per_chg_grossppe_f1 per_chg_pbdita_f1 pat_income per_chg_pbit_f1 add_total_ppe_assets per_chg_assets_f1, cuts(5 95)

eststo clear
label variable total_loan_yearly_ratio "Exposure to CEO changed bank"
label variable exp_low "Low-quality firm x Exposure to CEO changed bank"
label variable low_icr "Low-quality firm"

reghdfe lg_add  exp_low_any low_icr perc_loan_treat_any  pat_income_w lg_income lg_assets, absorb(sa_finance1_cocode year) cluster(nic_prod_code)
outreg2 using "Referee/tables/table_PPE.xls", append label ctitle (Log(Total Additions to Gross Fixed Assets)) dec(4) se coefastr bracket nocons addtext (Controls, Yes, Firm Fixed Effects, Yes, Year Fixed Effects, Yes)

reghdfe per_chg_pbdita_f1_w   exp_low_any low_icr perc_loan_treat_any  pat_income_w lg_income lg_assets, absorb(sa_finance1_cocode year) cluster(nic_prod_code)
outreg2 using "Referee/tables/table_PPE.xls", append label ctitle (PBDITA) dec(4) se coefastr bracket nocons addtext (Controls, Yes, Firm Fixed Effects, Yes, Year Fixed Effects, Yes)



****************************************************************************************************************************************************
/* Table 7 (Col. 1, 2 & 3) -  Low Capital Banks */
****************************************************************************************************************************************************

use "dta/capital_adequacy_ratio.dta", replace

keep if year == 2007

collapse (mean) sa_bd_cap_adeq_ratio, by(creditorcode)

merge m:1 creditorcode using "dta/public_sector_banks.dta"
keep if _m == 3
drop _m

egen low_capital = xtile(sa_bd_cap_adeq_ratio), n(2) 
replace low_capital = 2-low_capital

egen mean_capital = mean(sa_bd_cap_adeq_ratio)
gen low_capital_mean = 1 if sa_bd_cap_adeq_ratio < mean_capital
replace low_capital_mean = 0 if sa_bd_cap_adeq_ratio >= mean_capital

egen p33 = pctile(sa_bd_cap_adeq_ratio), p(33)
gen low_capital_p33 = 1 if sa_bd_cap_adeq_ratio < p33
replace low_capital_p33 = 0 if sa_bd_cap_adeq_ratio >= p33

save "dta/low_capital_banks.dta", replace

use "dta/tables.dta", replace

drop int_treat1_risky1

replace treat1 = 0 if creditorcode == 236337

gen int_treat1_risky1 = treat1*risky_firm1

// Bank*Firm groups
egen bank_firm = group(creditorcode sa_finance1_cocode)

// Bank*Time groups
egen bank_year = group(creditorcode year)

merge m:1 creditorcode using "dta/low_capital_banks.dta"
drop if _m == 2
drop _m

gen firmCode = sa_finance1_cocode
gen creditorCode = creditorcode
merge m:1 firmCode creditorCode year using "dta/FirmBankYearControls_2018 - Copy.dta"
drop if _m == 2
drop _m

merge m:1 sa_finance1_cocode year using "dta/firm_year_controls.dta"
drop if _m == 2
drop _m

gen lag_amount = exp(lag_lg_amount)
gen lag_amount_assets = lag_amount/(sa_total_assets*1000000)

// Bank*Time groups
egen bank_quarter = group(creditorcode period_new)

gen triple = int_treat1_risky1*low_capital_mean
gen treat_capital = treat1*low_capital_mean
gen risky_capital = risky_firm1*low_capital_mean

eststo clear
label var total_loan_given_dummy "Loan Indicator"
label var lg_loan "Log(Loan Amount)"
label var triple "CEO change x Low-quality firm x Low Capital"
label var treat_capital "CEO change x Low Capital"
label var risky_capital "Low-quality firm x Low Capital"
label var int_treat1_risky1 "CEO change x Low-quality firm"
label var treat1 "CEO change"
label var risky_firm1 "Low-quality firm"

reghdfe total_loan_given_dummy triple int_treat1_risky1 risky_capital treat_capital treat1 risky_firm1 low_capital_mean avg_loan_duration loan_completed_5 lag_amount_assets if period_new >= 28 & period_new <= 33, absorb(creditorcode firm_year) cluster(nic_prod_code)
outreg2 using "Referee/tables/table_low_capital.xls", replace label ctitle (Loan Dummy) dec(4) se coefastr bracket nocons addtext (Controls, Yes, Bank Fixed Effects, Yes, Bank-Quarter Fixed Effects, No, Firm x Year Fixed Effects, Yes)

reghdfe total_loan_given_dummy triple int_treat1_risky1 risky_capital treat_capital treat1 risky_firm1 low_capital_mean avg_loan_duration loan_completed_5 lag_amount_assets if period_new >= 28 & period_new <= 33, absorb(bank_quarter firm_year) cluster(nic_prod_code)
outreg2 using "Referee/tables/table_low_capital.xls", append label ctitle (Loan Dummy) dec(4) se coefastr bracket nocons addtext (Controls, Yes, Bank Fixed Effects, No, Bank-Quarter Fixed Effects, Yes, Firm x Year Fixed Effects, Yes)

reghdfe total_loan_given_dummy triple int_treat1_risky1 risky_capital treat_capital treat1 risky_firm1 low_capital_mean avg_loan_duration loan_completed_5 lag_amount_assets if period_new >= 28 & period_new <= 33, absorb(bank_quarter firm_year bank_firm) cluster(nic_prod_code)
outreg2 using "Referee/tables/table_low_capital.xls", append label ctitle (Loan Dummy) dec(4) se coefastr bracket nocons addtext (Controls, Yes, Bank x Firm Fixed Effects, Yes, Bank-Quarter Fixed Effects, Yes, Firm x Year Fixed Effects, Yes)

****************************************************************************************************************************************************
/* Table 7 (Col. 4, 5 & 6) -  Low Capital Banks */
****************************************************************************************************************************************************

use "dta/restructuring_trimmed.dta", replace

// Bank*Firm groups
egen bank_firm = group(creditorcode sa_finance1_cocode)

// Bank*Time groups
egen bank_year = group(creditorcode year)

merge m:1 creditorcode using "dta/low_capital_banks.dta"
drop if _m == 2
drop _m

gen firmCode = sa_finance1_cocode
gen creditorCode = creditorcode
merge m:1 firmCode creditorCode year using "dta/FirmBankYearControls_2018 - Copy.dta"
drop if _m == 2
drop _m

merge m:1 sa_finance1_cocode year using "dta/firm_year_controls.dta"
drop if _m == 2
drop _m

gen lag_amount = exp(lag_lg_amount)
gen lag_amount_assets = lag_amount/(sa_total_assets*1000000)

// Bank*Time groups
egen bank_quarter = group(creditorcode period_new)

gen triple = int_treat1_risky1*low_capital_mean
gen treat_capital = treat1*low_capital_mean
gen risky_capital = risky_firm1*low_capital_mean

eststo clear
label var triple "CEO change x Low-quality firm x Low Capital"
label var treat_capital "CEO change x Low Capital"
label var risky_capital "Low-quality firm x Low Capital"
label var int_treat1_risky1 "CEO change x Low-quality firm"
label var treat1 "CEO change"
label var risky_firm1 "Low-quality firm"

reghdfe num_of_modifications_dummy triple int_treat1_risky1 risky_capital treat_capital treat1 risky_firm1 low_capital_mean avg_loan_duration loan_completed_5 lag_amount_assets if period_new >= 28 & period_new <= 33, absorb(creditorcode firm_year) cluster(nic_prod_code)
outreg2 using "Referee/tables/table_low_capital.xls", append label ctitle (Restructuring) dec(4) se coefastr bracket nocons addtext (Controls, Yes, Bank Fixed Effects, Yes, Bank-Quarter Fixed Effects, No, Firm x Year Fixed Effects, Yes)

reghdfe num_of_modifications_dummy triple int_treat1_risky1 risky_capital treat_capital treat1 risky_firm1 low_capital_mean avg_loan_duration loan_completed_5 lag_amount_assets if period_new >= 28 & period_new <= 33, absorb(bank_quarter firm_year) cluster(nic_prod_code)
outreg2 using "Referee/tables/table_low_capital.xls", append label ctitle (Restructuring) dec(4) se coefastr bracket nocons addtext (Controls, Yes, Bank Fixed Effects, No, Bank-Quarter Fixed Effects, Yes, Firm x Year Fixed Effects, Yes)

reghdfe num_of_modifications_dummy triple int_treat1_risky1 risky_capital treat_capital treat1 risky_firm1 low_capital_mean avg_loan_duration loan_completed_5 lag_amount_assets if period_new >= 28 & period_new <= 33, absorb(bank_quarter firm_year bank_firm) cluster(nic_prod_code)
outreg2 using "Referee/tables/table_low_capital.xls", append label ctitle (Restructuring) dec(4) se coefastr bracket nocons addtext (Controls, Yes, Bank x Firm Fixed Effects, Yes, Bank-Quarter Fixed Effects, Yes, Firm x Year Fixed Effects, Yes)



********************************************************* Data Creation - Table 8 (Post Retirement Placement) (Begin) *******************************

import delimited "raw\Chairman firm-year data for matching.csv", encoding(ISO-8859-9) clear

save "dta/chairman_firm_year_matching.dta", replace

import delimited "raw\bank_info_next_position.txt", clear

destring sa_total_income-sa_net_int_margin, force replace

tostring sa_finance1_year, force replace
gen day = substr(sa_finance1_year,7,2)
gen month = substr(sa_finance1_year,5,2)
gen year = substr(sa_finance1_year,1,4)

destring day-year, force replace

duplicates tag sa_finance1_cocode year, gen(x)
bysort sa_finance1_cocode: gen march = 1 if x>0 & month == 3
bysort sa_finance1_cocode: egen max_march = max(march) if x>0
drop if x>0 & max_march == 1 & march != 1
sort sa_finance1_cocode year month
bysort sa_finance1_cocode year: gen dup_n = _n if x > 0 & max_march != 1 
drop if dup_n > 1 & dup_n != .
sort sa_finance1_cocode year month day
bysort sa_finance1_cocode year: egen march_dup = sum(march) if x > 0 & max_march == 1 
bysort sa_finance1_cocode year: gen march_dup_n = _n if march_dup > 1 & march_dup != .
bysort sa_finance1_cocode year: gen march_dup_total_n = _N if march_dup > 1 & march_dup != .
drop if march_dup_n < march_dup_total_n & march_dup_n != .

drop x-march_dup_total_n

tsset sa_finance1_cocode year, yearly

rename sa_finance1_cocode creditorcode 

save "dta/bank_info_next_position.dta", replace

import delimited "raw\Nifty 50 Historical Data.csv", varnames(1) encoding(UTF-8) clear

gen day = substr(date,1,2)
gen month = substr(date,4,2)
gen year = substr(date,7,4)

keep price day-year

destring day-year, force replace

keep if month == 3

bysort year: egen max_day = max(day)

keep if max_day == day

rename price nifty50_price

keep nifty50_price year

save "dta/nifty50_data.dta", replace

import delimited "raw\Nifty Bank Historical Data.csv", varnames(1) encoding(UTF-8) clear

gen day = substr(date,1,2)
gen month = substr(date,4,2)
gen year = substr(date,7,4)

keep price day-year

destring day-year, force replace

keep if month == 3

bysort year: egen max_day = max(day)

keep if max_day == day

rename price niftybank_price

keep niftybank_price year

save "dta/niftybank_data.dta", replace

import delimited "raw\stock_data_banks.txt", clear

tostring co_stkdate, force replace

gen year = substr(co_stkdate,1,4)
gen month = substr(co_stkdate,5,2)
gen day = substr(co_stkdate,7,2)

destring year-day, force replace

// merge m:1 year month day using "dta2/nifty50_data.dta"
// drop if _m==2
// drop _m

keep if month == 3

bysort co_code year: egen max_day = max(day)

keep if max_day == day


destring bse_closing_price, force replace

rename co_code creditorcode

keep creditorcode year bse_closing_price

save "dta/stock_data_banks.dta", replace



import delimited "raw\stock_data_banks.txt", clear

tostring co_stkdate, force replace

gen year = substr(co_stkdate,1,4)
gen month = substr(co_stkdate,5,2)
gen day = substr(co_stkdate,7,2)

destring year-day, force replace

replace year = year + 1 if month > 3
keep if year >= 1998 & year <= 2013
bysort co_code year: gen count = _N
duplicates drop co_code  year, force
bysort co_code: egen mean_days = mean(count)
duplicates drop co_code, force
br co_code mean_days
drop if mean_days < 240
keep co_code
rename co_code creditorcode

save "dta/banks_traded_frequently.dta", replace

********************************************************* Data Creation - Table 8 (Post Retirement Placement) (End) *******************************



****************************************************************************************************************************************************
/* Table 8 - Post Retirement Placement */
****************************************************************************************************************************************************

use "dta/bank_info_next_position.dta", replace

merge m:1 year using "dta/nifty50_data.dta"
drop if _m==2
drop _m

merge m:1 year using "dta/niftybank_data.dta"
drop if _m==2
drop _m

merge m:1 creditorcode year using "dta/stock_data_banks.dta"
drop if _m==2
drop _m

merge m:1 creditorcode year using "dta/chairman_firm_year_matching.dta"

bysort creditorcode chairman: egen max_year = max(year) if _m==3
bysort creditorcode chairman: egen min_year = min(year) if _m==3

bysort creditorcode: egen max_max_year = max(max_year)
bysort creditorcode: egen min_min_year = min(min_year)

keep if (year >= (min_min_year - 1)) & (year <= max_max_year)

bysort creditorcode chairman: gen count = _N

tsset creditorcode year

keep if (year == max_year) | (year == (min_min_year - 1))

sort creditorcode year

gen pbt_growth = (((sa_pbt/sa_pbt[_n-1])^(1/count)) - 1)

keep if _m==3
drop _m

summ pbt_growth, d

xtile pbt_growth_med = pbt_growth, nq(2)
replace pbt_growth_med = pbt_growth_med - 1

summ pbt_growth_med, d
summ pbt_growth_med if during_covid != 1, d
summ sig_pos  if during_covid != 1, d

reghdfe sig_pos pbt_growth_med , absorb(creditorcode min_year )
outreg2 using "tables/table_reg_ceo_next_position.xls", replace label ctitle (Post Retirement Position Dummy) dec(4) tstat coefastr bracket nocons addtext (Bank Fixed Effects, Yes, Joining Year Fixed Effects, Yes)
reghdfe sig_pos pbt_growth_med if during_covid != 1, absorb(creditorcode min_year)
outreg2 using "tables/table_reg_ceo_next_position.xls", append label ctitle (Post Retirement Position Dummy) dec(4) tstat coefastr bracket nocons addtext (Bank Fixed Effects, Yes, Joining Year Fixed Effects, Yes)



****************************************************************************************************************************************************
/* Table A4 - Comparing Treated vs Control Banks */
****************************************************************************************************************************************************

use "dta/capital_adequacy_ratio.dta", replace

merge m:1 creditorcode using "dta/public_sector_banks.dta"

keep if _m==3
drop _m

merge m:1 creditorcode using "dta/ceo_change_quarterly.dta"
gen ceo_change = 0 
replace ceo_change = 1 if _m==3 
replace ceo_change = 0 if creditorcode == 236337

gen log_income_year = log(1 + sa_total_income)
gen log_pat_year = log(1 + sa_pat )
gen log_pbt_year = log(1 + sa_pbt)
gen log_pbit_year = log(1 + sa_pbit )
gen log_cashprofit_year = log(1 + sa_cash_profit )
gen log_gnpa_open = log(1 + sa_bd_gross_npa_opening_bal)
gen log_gnpa_close = log(1 + sa_bd_gross_npa_closing_bal)
gen log_nnpa_open = log(1 + sa_bd_net_npa_opening_bal)
gen log_nnpa_close = log(1 + sa_bd_net_npa_closing_bal)
gen log_prov_npa = log(1 + sa_bd_prov_for_npa)
gen pat_income = sa_pat/sa_total_income
gen pbt_income = sa_pbt/sa_total_income
gen pbit_income = sa_pbit/sa_total_income
gen cash_profit_income = sa_cash_profit/sa_total_income
gen gnpa_open_income = sa_bd_gross_npa_opening_bal/sa_total_income
gen gnpa_close_income = sa_bd_gross_npa_closing_bal/sa_total_income
gen nnpa_open_income = sa_bd_net_npa_opening_bal/sa_total_income
gen nnpa_close_income = sa_bd_net_npa_closing_bal/sa_total_income
gen prov_npa_income = sa_bd_prov_for_npa/sa_total_income
gen gnpa_open_loan_adv = sa_bd_gross_npa_opening_bal/sa_total_loans_and_advances
gen gnpa_close_loan_adv = sa_bd_gross_npa_closing_bal/sa_total_loans_and_advances
gen nnpa_open_loan_adv = sa_bd_net_npa_opening_bal/sa_total_loans_and_advances
gen nnpa_close_loan_adv = sa_bd_net_npa_closing_bal/sa_total_loans_and_advances
gen prov_npa_loan_adv = sa_bd_prov_for_npa/sa_total_loans_and_advances
gen gnpa_open_dep_adv = sa_bd_gross_npa_opening_bal/sa_deposits_n_advances
gen gnpa_close_dep_adv = sa_bd_gross_npa_closing_bal/sa_deposits_n_advances
gen nnpa_open_dep_adv = sa_bd_net_npa_opening_bal/sa_deposits_n_advances
gen nnpa_close_dep_adv = sa_bd_net_npa_closing_bal/sa_deposits_n_advances
gen prov_npa_dep_adv = sa_bd_prov_for_npa/sa_deposits_n_advances
gen gnpa_open_othloan_adv = sa_bd_gross_npa_opening_bal/sa_oth_loans_and_advances
gen gnpa_close_othloan_adv = sa_bd_gross_npa_closing_bal/sa_oth_loans_and_advances
gen nnpa_open_othloan_adv = sa_bd_net_npa_opening_bal/sa_oth_loans_and_advances
gen nnpa_close_othloan_adv = sa_bd_net_npa_closing_bal/sa_oth_loans_and_advances
gen prov_npa_othloan_adv = sa_bd_prov_for_npa/sa_oth_loans_and_advances

tsset creditorcode year, yearly

gen loan_growth = (sa_oth_loans_and_advances - L1.sa_oth_loans_and_advances)/L1.sa_oth_loans_and_advances

drop if creditorcode == 100283

ttest log_income_year if (year == 2007 | year == 2006 | year == 2005), by(ceo_change)
ttest log_pat_year if (year == 2007 | year == 2006 | year == 2005), by(ceo_change)
ttest sa_bd_cap_adeq_ratio if (year == 2007 | year == 2006 | year == 2005), by(ceo_change)
ttest loan_growth if (year == 2007 | year == 2006 | year == 2005), by(ceo_change)
ttest gnpa_close_loan_adv if (year == 2007 | year == 2006 | year == 2005), by(ceo_change)



********************************************************* Data Creation - Table A5 (Placebo - Panel A) (Begin) **********************************

use "dta/ceo_change_quarterly.dta", replace

replace period_ceo = period_ceo - 8

save "dta/ceo_change_quarterly_placebo1.dta", replace

import delimited "raw\firms_interest_coverage.txt", delimiter("|") clear

destring sa_total_income-sa_debt_equity, force replace

tostring sa_finance1_year, force replace
gen day = substr(sa_finance1_year,7,2)
gen month = substr(sa_finance1_year,5,2)
gen year = substr(sa_finance1_year,1,4)

destring day-year, force replace

duplicates tag sa_finance1_cocode year, gen(x)
bysort sa_finance1_cocode: gen march = 1 if x>0 & month == 3
bysort sa_finance1_cocode: egen max_march = max(march) if x>0
drop if x>0 & max_march == 1 & march != 1
sort sa_finance1_cocode year month
bysort sa_finance1_cocode year: gen dup_n = _n if x > 0 & max_march != 1 
drop if dup_n > 1 & dup_n != .
sort sa_finance1_cocode year month day
bysort sa_finance1_cocode year: egen march_dup = sum(march) if x > 0 & max_march == 1 
bysort sa_finance1_cocode year: gen march_dup_n = _n if march_dup > 1 & march_dup != .
bysort sa_finance1_cocode year: gen march_dup_total_n = _N if march_dup > 1 & march_dup != .
drop if march_dup_n < march_dup_total_n & march_dup_n != .

drop x-march_dup_total_n

merge m:1 sa_finance1_cocode using "dta/industry_code.dta"
keep if _m==3
drop _m

keep if year == 2005

gen int_cov_ratio = sa_pbit/sa_interest_exp
drop if int_cov_ratio == .
bysort sa_finance1_cocode: egen avg_int_cov_ratio = mean(int_cov_ratio)
gen risky_firm1 = 0
replace risky_firm1 = 1 if avg_int_cov_ratio < 1

gen low_int_cov_ratio = 0
replace low_int_cov_ratio = 1 if int_cov_ratio < 1

bysort sa_finance1_cocode: egen risky_firm2 = max(low_int_cov_ratio)
bysort sa_finance1_cocode: egen risky_firm3 = min(low_int_cov_ratio)

ttest sa_secured_borrowings , by(risky_firm1)
ttest sa_short_term_bank_borrowings , by(risky_firm1)
ttest sa_long_term_bank_borrowings , by(risky_firm1)
ttest sa_quick_ratio , by(risky_firm1)
ttest sa_current_ratio , by(risky_firm1)
ttest sa_debt_equity , by(risky_firm1)

duplicates drop sa_finance1_cocode, force

keep sa_finance1_cocode co_industry_name sa_company_name risky_firm1 risky_firm2 risky_firm3 avg_int_cov_ratio

save "dta/firms_interest_coverage_placebo1.dta", replace

use "dta/MCA_data_psb_year_quarter_final.dta", replace

keep if period_new >= 1 & period_new <= 18
keep if total_loan_given != 0
duplicates drop sa_finance1_cocode creditorcode, force

keep sa_finance1_cocode creditorcode 

distinct sa_finance1_cocode
distinct creditorcode

save "dta/prev_yrs_loan_firms_quarterly_placebo1.dta", replace

use "dta/MCA_data_psb_year_quarter_final.dta", replace

merge m:1 sa_finance1_cocode creditorcode using "dta/prev_yrs_loan_firms_quarterly_placebo1.dta"

keep if _m==3
drop _m

merge m:1 creditorcode using "dta/ceo_change_quarterly_placebo1.dta"

replace period_ceo = 10000 if _m==1
drop if _m==2 
drop _m

gen treat1 = 0
replace treat1 = 1 if period_new >= period_ceo

gen treat2 = 0
replace treat2 = 1 if period_new > period_ceo

bysort creditorcode: egen sum_treat1 = sum(treat1)

merge m:1 sa_finance1_cocode using "dta/firms_interest_coverage_placebo1.dta"
keep if _m==3
drop _m

merge m:1 sa_finance1_cocode using "dta/industry.dta"
drop if _m==2
drop _m

gen int_treat1_risky1 = treat1*risky_firm1
gen int_treat1_risky2 = treat1*risky_firm2
gen int_treat1_risky3 = treat1*risky_firm3
gen int_treat2_risky1 = treat2*risky_firm1
gen int_treat2_risky2 = treat2*risky_firm2
gen int_treat2_risky3 = treat2*risky_firm3

gen total_loan_given_dummy = 0
replace total_loan_given_dummy = 1 if total_loan_given > 0

egen firm_year = group(year sa_finance1_cocode)
egen firm_period = group(period_new  sa_finance1_cocode)
gen nic_prod_code_2 = substr(nic_prod_code,1,2)
gen nic_prod_code_3 = substr(nic_prod_code,1,3)

bysort creditorcode: egen first_period_ceo = min(period_new) if treat1 == 1
bysort creditorcode: egen min_first_period_ceo = min(first_period_ceo)

gen dist_from_ceo_change = period_new - min_first_period

gen dummy_pre1 = 0
replace dummy_pre1 = 1 if dist_from_ceo_change == -1
gen dummy_pre2 = 0
replace dummy_pre2 = 1 if dist_from_ceo_change == -2
gen dummy_pre3 = 0
replace dummy_pre3 = 1 if dist_from_ceo_change == -3
gen dummy_pre4 = 0
replace dummy_pre4 = 1 if dist_from_ceo_change == -4
gen dummy_pre5 = 0
replace dummy_pre5 = 1 if dist_from_ceo_change == -5
gen dummy_post = 0
replace dummy_post = 1 if dist_from_ceo_change >= 0 & dist_from_ceo_change != .

gen dummy_pre1_risky1 = dummy_pre1*risky_firm1
gen dummy_pre2_risky1 = dummy_pre2*risky_firm1
gen dummy_pre3_risky1 = dummy_pre3*risky_firm1
gen dummy_pre4_risky1 = dummy_pre4*risky_firm1
gen dummy_pre5_risky1 = dummy_pre5*risky_firm1
gen dummy_post_risky1 = dummy_post*risky_firm1

gen lg_loan = log(1+total_loan_amount)

summ total_loan_given if period_new >= 20 & period_new <= 25, d
summ total_loan_given_dummy if period_new >= 20 & period_new <= 25, d
summ total_loan_amount if period_new >= 20 & period_new <= 25, d

merge m:1 creditorcode year quarter using "dta/bank_info.dta"
drop if _m==2
drop _m

merge m:1 creditorcode year using "dta/capital_adequacy_ratio.dta"
drop if _m==2
drop _m

gen log_income = log(1 + siq_ntrm_total_inc)
gen log_pat = log(1 + siq_ntrm_after_tax_profit_loss_c )

gen log_income_year = log(1 + sa_total_income)
gen pat_income = sa_pat/sa_total_income
gen gnpa_close_loan_adv = sa_bd_gross_npa_closing_bal/sa_total_loans_and_advances

bysort creditorcode sa_finance1_cocode: egen min_period_new = min(period_new) if total_loan_given != 0
gen diff_min_period_new = period_new - min_period_new
replace diff_min_period_new = 0 if diff_min_period_new == . | diff_min_period_new < 0

save "dta/tables_placebo1.dta", replace

use "raw\LoansInMCA(2).dta", replace

keep if sanctionDate  >= td(01apr2001) & sanctionDate  <= td(30sep2005)
keep if modificationDate  >= td(01apr2001) & modificationDate  <= td(31mar2008)

rename (creditorCode firmCode) (creditorcode firmcode)

merge m:1 creditorcode using "dta/public_sector_banks1.dta"

keep if _m==3
drop _m

gen period = .

replace period = 1 if modificationDate >= td(01apr2001) & modificationDate <= td(30jun2001)
replace period = 2 if modificationDate >= td(01jul2001) & modificationDate <= td(30sep2001)
replace period = 3 if modificationDate >= td(01oct2001) & modificationDate <= td(31dec2001)
replace period = 4 if modificationDate >= td(01jan2002) & modificationDate <= td(31mar2002)
replace period = 5 if modificationDate >= td(01apr2002) & modificationDate <= td(30jun2002)
replace period = 6 if modificationDate >= td(01jul2002) & modificationDate <= td(30sep2002)
replace period = 7 if modificationDate >= td(01oct2002) & modificationDate <= td(31dec2002)
replace period = 8 if modificationDate >= td(01jan2003) & modificationDate <= td(31mar2003)
replace period = 9 if modificationDate >= td(01apr2003) & modificationDate <= td(30jun2003)
replace period = 10 if modificationDate >= td(01jul2003) & modificationDate <= td(30sep2003)
replace period = 11 if modificationDate >= td(01oct2003) & modificationDate <= td(31dec2003)
replace period = 12 if modificationDate >= td(01jan2004) & modificationDate <= td(31mar2004)
replace period = 13 if modificationDate >= td(01apr2004) & modificationDate <= td(30jun2004)
replace period = 14 if modificationDate >= td(01jul2004) & modificationDate <= td(30sep2004)
replace period = 15 if modificationDate >= td(01oct2004) & modificationDate <= td(31dec2004)
replace period = 16 if modificationDate >= td(01jan2005) & modificationDate <= td(31mar2005)
replace period = 17 if modificationDate >= td(01apr2005) & modificationDate <= td(30jun2005)
replace period = 18 if modificationDate >= td(01jul2005) & modificationDate <= td(30sep2005)
replace period = 19 if modificationDate >= td(01oct2005) & modificationDate <= td(31dec2005)
replace period = 20 if modificationDate >= td(01jan2006) & modificationDate <= td(31mar2006)
replace period = 21 if modificationDate >= td(01apr2006) & modificationDate <= td(30jun2006)
replace period = 22 if modificationDate >= td(01jul2006) & modificationDate <= td(30sep2006)
replace period = 23 if modificationDate >= td(01oct2006) & modificationDate <= td(31dec2006)
replace period = 24 if modificationDate >= td(01jan2007) & modificationDate <= td(31mar2007)
replace period = 25 if modificationDate >= td(01apr2007) & modificationDate <= td(30jun2007)
replace period = 26 if modificationDate >= td(01jul2007) & modificationDate <= td(30sep2007)
replace period = 27 if modificationDate >= td(01oct2007) & modificationDate <= td(31dec2007)
replace period = 28 if modificationDate >= td(01jan2008) & modificationDate <= td(31mar2008)
replace period = 29 if modificationDate >= td(01apr2008) & modificationDate <= td(30jun2008)
replace period = 30 if modificationDate >= td(01jul2008) & modificationDate <= td(30sep2008)
replace period = 31 if modificationDate >= td(01oct2008) & modificationDate <= td(31dec2008)
replace period = 32 if modificationDate >= td(01jan2009) & modificationDate <= td(31mar2009)
replace period = 33 if modificationDate >= td(01apr2009) & modificationDate <= td(30jun2009)
replace period = 34 if modificationDate >= td(01jul2009) & modificationDate <= td(30sep2009)
replace period = 35 if modificationDate >= td(01oct2009) & modificationDate <= td(31dec2009)
replace period = 36 if modificationDate >= td(01jan2010) & modificationDate <= td(31mar2010)

gen period_sanction = .

replace period_sanction = 1 if sanctionDate >= td(01apr2001) & sanctionDate <= td(30jun2001)
replace period_sanction = 2 if sanctionDate >= td(01jul2001) & sanctionDate <= td(30sep2001)
replace period_sanction = 3 if sanctionDate >= td(01oct2001) & sanctionDate <= td(31dec2001)
replace period_sanction = 4 if sanctionDate >= td(01jan2002) & sanctionDate <= td(31mar2002)
replace period_sanction = 5 if sanctionDate >= td(01apr2002) & sanctionDate <= td(30jun2002)
replace period_sanction = 6 if sanctionDate >= td(01jul2002) & sanctionDate <= td(30sep2002)
replace period_sanction = 7 if sanctionDate >= td(01oct2002) & sanctionDate <= td(31dec2002)
replace period_sanction = 8 if sanctionDate >= td(01jan2003) & sanctionDate <= td(31mar2003)
replace period_sanction = 9 if sanctionDate >= td(01apr2003) & sanctionDate <= td(30jun2003)
replace period_sanction = 10 if sanctionDate >= td(01jul2003) & sanctionDate <= td(30sep2003)
replace period_sanction = 11 if sanctionDate >= td(01oct2003) & sanctionDate <= td(31dec2003)
replace period_sanction = 12 if sanctionDate >= td(01jan2004) & sanctionDate <= td(31mar2004)
replace period_sanction = 13 if sanctionDate >= td(01apr2004) & sanctionDate <= td(30jun2004)
replace period_sanction = 14 if sanctionDate >= td(01jul2004) & sanctionDate <= td(30sep2004)
replace period_sanction = 15 if sanctionDate >= td(01oct2004) & sanctionDate <= td(31dec2004)
replace period_sanction = 16 if sanctionDate >= td(01jan2005) & sanctionDate <= td(31mar2005)
replace period_sanction = 17 if sanctionDate >= td(01apr2005) & sanctionDate <= td(30jun2005)
replace period_sanction = 18 if sanctionDate >= td(01jul2005) & sanctionDate <= td(30sep2005)
replace period_sanction = 19 if sanctionDate >= td(01oct2005) & sanctionDate <= td(31dec2005)
replace period_sanction = 20 if sanctionDate >= td(01jan2006) & sanctionDate <= td(31mar2006)
replace period_sanction = 21 if sanctionDate >= td(01apr2006) & sanctionDate <= td(30jun2006)
replace period_sanction = 22 if sanctionDate >= td(01jul2006) & sanctionDate <= td(30sep2006)
replace period_sanction = 23 if sanctionDate >= td(01oct2006) & sanctionDate <= td(31dec2006)
replace period_sanction = 24 if sanctionDate >= td(01jan2007) & sanctionDate <= td(31mar2007)
replace period_sanction = 25 if sanctionDate >= td(01apr2007) & sanctionDate <= td(30jun2007)
replace period_sanction = 26 if sanctionDate >= td(01jul2007) & sanctionDate <= td(30sep2007)
replace period_sanction = 27 if sanctionDate >= td(01oct2007) & sanctionDate <= td(31dec2007)
replace period_sanction = 28 if sanctionDate >= td(01jan2008) & sanctionDate <= td(31mar2008)
replace period_sanction = 29 if sanctionDate >= td(01apr2008) & sanctionDate <= td(30jun2008)
replace period_sanction = 30 if sanctionDate >= td(01jul2008) & sanctionDate <= td(30sep2008)
replace period_sanction = 31 if sanctionDate >= td(01oct2008) & sanctionDate <= td(31dec2008)
replace period_sanction = 32 if sanctionDate >= td(01jan2009) & sanctionDate <= td(31mar2009)
replace period_sanction = 33 if sanctionDate >= td(01apr2009) & sanctionDate <= td(30jun2009)
replace period_sanction = 34 if sanctionDate >= td(01jul2009) & sanctionDate <= td(30sep2009)
replace period_sanction = 35 if sanctionDate >= td(01oct2009) & sanctionDate <= td(31dec2009)
replace period_sanction = 36 if sanctionDate >= td(01jan2010) & sanctionDate <= td(31mar2010)

gen year = int((period-1)/4)
replace year = year + 2002
gen quarter = mod(period,4)
replace quarter = 4 if quarter == 0

bysort creditorcode firmcode year quarter: gen num_of_modifications = _N

gen same_period_modification = 0
replace same_period_modification = 1 if period == period_sanction

bysort creditorcode firmcode year quarter: egen num_same_modifications = sum(same_period_modification)
bysort creditorcode firmcode year quarter: egen loan_amount_modified = sum(loanAmount)

gen num_of_modifications2 = num_of_modifications - num_same_modifications

duplicates drop creditorcode firmcode year quarter, force

rename firmcode sa_finance1_cocode

keep creditorcode sa_finance1_cocode year quarter period num_of_modifications num_of_modifications2 loanAmount loan_amount_modified

save "dta/restructuring_psb_final_placebo1.dta", replace

********************************************************* Data Creation - Table A5 (Placebo - Panel A) (End) ************************************



****************************************************************************************************************************************************
/* Table A5 - Placebo (Panel A) */
****************************************************************************************************************************************************

use "dta/tables_placebo1.dta", replace

gen firmCode = sa_finance1_cocode
gen creditorCode = creditorcode
merge m:1 firmCode creditorCode year using "dta/FirmBankYearControls_2018 - Copy.dta"
drop if _m == 2
drop _m

merge m:1 sa_finance1_cocode year using "dta/firm_year_controls.dta"
drop if _m == 2
drop _m

gen lag_amount = exp(lag_lg_amount)
gen lag_amount_assets = lag_amount/(sa_total_assets*1000000)

// Bank*Time groups
egen bank_quarter = group(creditorcode period_new)

eststo clear
label var total_loan_given_dummy "Loan Indicator"
label var lg_loan "Log(Loan Amount)"
label var int_treat1_risky1 "CEO change x Low-quality firm"
label var treat1 "CEO change"
label var risky_firm1 "Low-quality firm"

merge m:1 sa_finance1_cocode creditorcode year quarter using "dta/restructuring_psb_final_placebo1.dta"

replace num_of_modifications = 0 if _m==1
replace num_of_modifications2 = 0 if _m==1

drop if _m==2
drop _m

gen num_of_modifications_dummy = 0
replace num_of_modifications_dummy = 1 if num_of_modifications > 0

gen num_of_modifications_dummy2 = 0
replace num_of_modifications_dummy2 = 1 if num_of_modifications2 > 0

reghdfe total_loan_given_dummy int_treat1_risky1 treat1 risky_firm1 avg_loan_duration loan_completed_5 lag_amount_assets if period_new >= 20 & period_new <= 25, absorb(creditorcode  firm_year) cluster(nic_prod_code)
outreg2 using "Referee/tables/placebo_reg.xls", replace label ctitle (Loan Dummy) dec(4) se coefastr bracket nocons addtext (Controls, Yes, Bank Fixed Effects, Yes, Bank x Quarter Fixed Effects, No, Firm x Year Fixed Effects, Yes, Bank-Firm Fixed Effects, No)

reghdfe total_loan_given_dummy int_treat1_risky1 treat1 risky_firm1 avg_loan_duration loan_completed_5 lag_amount_assets if period_new >= 20 & period_new <= 25, absorb(bank_quarter firm_year) cluster(nic_prod_code)
outreg2 using "Referee/tables/placebo_reg.xls", append label ctitle (Loan Dummy) dec(4) se coefastr bracket nocons addtext (Controls, Yes, Bank Fixed Effects, No, Bank x Quarter Fixed Effects, Yes, Firm x Year Fixed Effects, Yes, Bank-Firm Fixed Effects, No)

reghdfe total_loan_given_dummy int_treat1_risky1 treat1 risky_firm1 avg_loan_duration loan_completed_5 lag_amount_assets if period_new >= 20 & period_new <= 25, absorb(bank_quarter bank_firm  firm_year) cluster(nic_prod_code)
outreg2 using "Referee/tables/placebo_reg.xls", append label ctitle (Loan Dummy) dec(4) se coefastr bracket nocons addtext (Controls, Yes, Bank Fixed Effects, No, Bank x Quarter Fixed Effects, Yes, Firm x Year Fixed Effects, Yes, Bank-Firm Fixed Effects, Yes)

reghdfe num_of_modifications_dummy  int_treat1_risky1 treat1 risky_firm1 avg_loan_duration loan_completed_5 lag_amount_assets if period_new >= 20 & period_new <= 25, absorb(creditorcode  firm_year) cluster(nic_prod_code)
outreg2 using "Referee/tables/placebo_reg.xls", append label ctitle (Restructuring) dec(4) se coefastr bracket nocons addtext (Controls, Yes, Bank Fixed Effects, Yes, Bank x Quarter Fixed Effects, No, Firm x Year Fixed Effects, Yes, Bank-Firm Fixed Effects, No)

reghdfe num_of_modifications_dummy  int_treat1_risky1 treat1 risky_firm1 avg_loan_duration loan_completed_5 lag_amount_assets if period_new >= 20 & period_new <= 25, absorb(bank_quarter firm_year) cluster(nic_prod_code)
outreg2 using "Referee/tables/placebo_reg.xls", append label ctitle (Restructuring) dec(4) se coefastr bracket nocons addtext (Controls, Yes, Bank Fixed Effects, No, Bank x Quarter Fixed Effects, Yes, Firm x Year Fixed Effects, Yes, Bank-Firm Fixed Effects, No)

reghdfe num_of_modifications_dummy  int_treat1_risky1 treat1 risky_firm1 avg_loan_duration loan_completed_5 lag_amount_assets if period_new >= 20 & period_new <= 25, absorb(bank_quarter bank_firm  firm_year) cluster(nic_prod_code)
outreg2 using "Referee/tables/placebo_reg.xls", append label ctitle (Restructuring) dec(4) se coefastr bracket nocons addtext (Controls, Yes, Bank Fixed Effects, No, Bank x Quarter Fixed Effects, Yes, Firm x Year Fixed Effects, Yes, Bank-Firm Fixed Effects, Yes)



********************************************************* Data Creation - Table A5 (Placebo - Panel B) (Begin) **********************************

use "dta/MCA_data_with_dates.dta", replace

keep if sanctionDate >= td(01apr1999) & sanctionDate <= td(31mar2008)

duplicates drop creditorCode firmCode, force

rename (creditorCode firmCode) (creditorcode firmcode)

merge m:1 creditorcode using "dta/public_sector_banks.dta"

keep if _m==3
drop _m

keep creditorName creditorcode firmcode firmName psb_name

save "dta/MCA_data_psb_dta_placebo2.dta", replace

use "dta/MCA_data_psb_dta_placebo2.dta", replace
append using "dta/MCA_data_psb_dta_placebo2.dta"
append using "dta/MCA_data_psb_dta_placebo2.dta"
append using "dta/MCA_data_psb_dta_placebo2.dta"

bysort creditorcode firmcode: gen quarter = _n

save "dta/MCA_data_psb_only_quarter_placebo2.dta", replace

use "dta/MCA_data_psb_only_quarter_placebo2.dta", replace
append using "dta/MCA_data_psb_only_quarter_placebo2.dta"
append using "dta/MCA_data_psb_only_quarter_placebo2.dta"
append using "dta/MCA_data_psb_only_quarter_placebo2.dta"
append using "dta/MCA_data_psb_only_quarter_placebo2.dta"
append using "dta/MCA_data_psb_only_quarter_placebo2.dta"
append using "dta/MCA_data_psb_only_quarter_placebo2.dta"
append using "dta/MCA_data_psb_only_quarter_placebo2.dta"
append using "dta/MCA_data_psb_only_quarter_placebo2.dta"

bysort creditorcode firmcode quarter: gen year = _n

replace year = year + 1999

save "dta/MCA_data_psb_year_quarter_blank_placebo2.dta", replace

use "dta/MCA_data_with_dates.dta", replace

keep if sanctionDate >= td(01apr1999) & sanctionDate <= td(31mar2008)

rename (creditorCode firmCode) (creditorcode firmcode)

merge m:1 creditorcode using "dta/public_sector_banks.dta"

keep if _m==3
drop _m

gen period = .

replace period = 1 if sanctionDate >= td(01apr1999) & sanctionDate <= td(30jun1999)
replace period = 2 if sanctionDate >= td(01jul1999) & sanctionDate <= td(30sep1999)
replace period = 3 if sanctionDate >= td(01oct1999) & sanctionDate <= td(31dec1999)
replace period = 4 if sanctionDate >= td(01jan2000) & sanctionDate <= td(31mar2000)
replace period = 5 if sanctionDate >= td(01apr2000) & sanctionDate <= td(30jun2000)
replace period = 6 if sanctionDate >= td(01jul2000) & sanctionDate <= td(30sep2000)
replace period = 7 if sanctionDate >= td(01oct2000) & sanctionDate <= td(31dec2000)
replace period = 8 if sanctionDate >= td(01jan2001) & sanctionDate <= td(31mar2001)
replace period = 9 if sanctionDate >= td(01apr2001) & sanctionDate <= td(30jun2001)
replace period = 10 if sanctionDate >= td(01jul2001) & sanctionDate <= td(30sep2001)
replace period = 11 if sanctionDate >= td(01oct2001) & sanctionDate <= td(31dec2001)
replace period = 12 if sanctionDate >= td(01jan2002) & sanctionDate <= td(31mar2002)
replace period = 13 if sanctionDate >= td(01apr2002) & sanctionDate <= td(30jun2002)
replace period = 14 if sanctionDate >= td(01jul2002) & sanctionDate <= td(30sep2002)
replace period = 15 if sanctionDate >= td(01oct2002) & sanctionDate <= td(31dec2002)
replace period = 16 if sanctionDate >= td(01jan2003) & sanctionDate <= td(31mar2003)
replace period = 17 if sanctionDate >= td(01apr2003) & sanctionDate <= td(30jun2003)
replace period = 18 if sanctionDate >= td(01jul2003) & sanctionDate <= td(30sep2003)
replace period = 19 if sanctionDate >= td(01oct2003) & sanctionDate <= td(31dec2003)
replace period = 20 if sanctionDate >= td(01jan2004) & sanctionDate <= td(31mar2004)
replace period = 21 if sanctionDate >= td(01apr2004) & sanctionDate <= td(30jun2004)
replace period = 22 if sanctionDate >= td(01jul2004) & sanctionDate <= td(30sep2004)
replace period = 23 if sanctionDate >= td(01oct2004) & sanctionDate <= td(31dec2004)
replace period = 24 if sanctionDate >= td(01jan2005) & sanctionDate <= td(31mar2005)
replace period = 25 if sanctionDate >= td(01apr2005) & sanctionDate <= td(30jun2005)
replace period = 26 if sanctionDate >= td(01jul2005) & sanctionDate <= td(30sep2005)
replace period = 27 if sanctionDate >= td(01oct2005) & sanctionDate <= td(31dec2005)
replace period = 28 if sanctionDate >= td(01jan2006) & sanctionDate <= td(31mar2006)
replace period = 29 if sanctionDate >= td(01apr2006) & sanctionDate <= td(30jun2006)
replace period = 30 if sanctionDate >= td(01jul2006) & sanctionDate <= td(30sep2006)
replace period = 31 if sanctionDate >= td(01oct2006) & sanctionDate <= td(31dec2006)
replace period = 32 if sanctionDate >= td(01jan2007) & sanctionDate <= td(31mar2007)
replace period = 33 if sanctionDate >= td(01apr2007) & sanctionDate <= td(30jun2007)
replace period = 34 if sanctionDate >= td(01jul2007) & sanctionDate <= td(30sep2007)
replace period = 35 if sanctionDate >= td(01oct2007) & sanctionDate <= td(31dec2007)
replace period = 36 if sanctionDate >= td(01jan2008) & sanctionDate <= td(31mar2008)

gen period_repayment = .

replace period_repayment = 1 if repaymentDate >= td(01apr1999) & repaymentDate <= td(30jun1999)
replace period_repayment = 2 if repaymentDate >= td(01jul1999) & repaymentDate <= td(30sep1999)
replace period_repayment = 3 if repaymentDate >= td(01oct1999) & repaymentDate <= td(31dec1999)
replace period_repayment = 4 if repaymentDate >= td(01jan2000) & repaymentDate <= td(31mar2000)
replace period_repayment = 5 if repaymentDate >= td(01apr2000) & repaymentDate <= td(30jun2000)
replace period_repayment = 6 if repaymentDate >= td(01jul2000) & repaymentDate <= td(30sep2000)
replace period_repayment = 7 if repaymentDate >= td(01oct2000) & repaymentDate <= td(31dec2000)
replace period_repayment = 8 if repaymentDate >= td(01jan2001) & repaymentDate <= td(31mar2001)
replace period_repayment = 9 if repaymentDate >= td(01apr2001) & repaymentDate <= td(30jun2001)
replace period_repayment = 10 if repaymentDate >= td(01jul2001) & repaymentDate <= td(30sep2001)
replace period_repayment = 11 if repaymentDate >= td(01oct2001) & repaymentDate <= td(31dec2001)
replace period_repayment = 12 if repaymentDate >= td(01jan2002) & repaymentDate <= td(31mar2002)
replace period_repayment = 13 if repaymentDate >= td(01apr2002) & repaymentDate <= td(30jun2002)
replace period_repayment = 14 if repaymentDate >= td(01jul2002) & repaymentDate <= td(30sep2002)
replace period_repayment = 15 if repaymentDate >= td(01oct2002) & repaymentDate <= td(31dec2002)
replace period_repayment = 16 if repaymentDate >= td(01jan2003) & repaymentDate <= td(31mar2003)
replace period_repayment = 17 if repaymentDate >= td(01apr2003) & repaymentDate <= td(30jun2003)
replace period_repayment = 18 if repaymentDate >= td(01jul2003) & repaymentDate <= td(30sep2003)
replace period_repayment = 19 if repaymentDate >= td(01oct2003) & repaymentDate <= td(31dec2003)
replace period_repayment = 20 if repaymentDate >= td(01jan2004) & repaymentDate <= td(31mar2004)
replace period_repayment = 21 if repaymentDate >= td(01apr2004) & repaymentDate <= td(30jun2004)
replace period_repayment = 22 if repaymentDate >= td(01jul2004) & repaymentDate <= td(30sep2004)
replace period_repayment = 23 if repaymentDate >= td(01oct2004) & repaymentDate <= td(31dec2004)
replace period_repayment = 24 if repaymentDate >= td(01jan2005) & repaymentDate <= td(31mar2005)
replace period_repayment = 25 if repaymentDate >= td(01apr2005) & repaymentDate <= td(30jun2005)
replace period_repayment = 26 if repaymentDate >= td(01jul2005) & repaymentDate <= td(30sep2005)
replace period_repayment = 27 if repaymentDate >= td(01oct2005) & repaymentDate <= td(31dec2005)
replace period_repayment = 28 if repaymentDate >= td(01jan2006) & repaymentDate <= td(31mar2006)
replace period_repayment = 29 if repaymentDate >= td(01apr2006) & repaymentDate <= td(30jun2006)
replace period_repayment = 30 if repaymentDate >= td(01jul2006) & repaymentDate <= td(30sep2006)
replace period_repayment = 31 if repaymentDate >= td(01oct2006) & repaymentDate <= td(31dec2006)
replace period_repayment = 32 if repaymentDate >= td(01jan2007) & repaymentDate <= td(31mar2007)
replace period_repayment = 33 if repaymentDate >= td(01apr2007) & repaymentDate <= td(30jun2007)
replace period_repayment = 34 if repaymentDate >= td(01jul2007) & repaymentDate <= td(30sep2007)
replace period_repayment = 35 if repaymentDate >= td(01oct2007) & repaymentDate <= td(31dec2007)
replace period_repayment = 36 if repaymentDate >= td(01jan2008) & repaymentDate <= td(31mar2008)

gen year = int((period-1)/4)
replace year = year + 2000
gen quarter = mod(period,4)
replace quarter = 4 if quarter == 0

gen zero_loan_amount = 0
replace zero_loan_amount = 1 if loanAmount == 0

bysort creditorcode firmcode year quarter: egen total_loan_amount = sum(loanAmount)
bysort creditorcode firmcode year quarter: egen total_loan_given = sum(psb)

duplicates drop creditorcode firmcode year quarter, force

save "dta/MCA_data_psb_quarter_included_placebo2.dta", replace

use "dta/ceo_change_quarterly.dta", replace

replace period_ceo = period_ceo - 16

save "dta/ceo_change_quarterly_placebo2.dta", replace

import delimited "raw\firms_interest_coverage.txt", delimiter("|") clear

destring sa_total_income-sa_debt_equity, force replace

tostring sa_finance1_year, force replace
gen day = substr(sa_finance1_year,7,2)
gen month = substr(sa_finance1_year,5,2)
gen year = substr(sa_finance1_year,1,4)

destring day-year, force replace

duplicates tag sa_finance1_cocode year, gen(x)
bysort sa_finance1_cocode: gen march = 1 if x>0 & month == 3
bysort sa_finance1_cocode: egen max_march = max(march) if x>0
drop if x>0 & max_march == 1 & march != 1
sort sa_finance1_cocode year month
bysort sa_finance1_cocode year: gen dup_n = _n if x > 0 & max_march != 1 
drop if dup_n > 1 & dup_n != .
sort sa_finance1_cocode year month day
bysort sa_finance1_cocode year: egen march_dup = sum(march) if x > 0 & max_march == 1 
bysort sa_finance1_cocode year: gen march_dup_n = _n if march_dup > 1 & march_dup != .
bysort sa_finance1_cocode year: gen march_dup_total_n = _N if march_dup > 1 & march_dup != .
drop if march_dup_n < march_dup_total_n & march_dup_n != .

drop x-march_dup_total_n

merge m:1 sa_finance1_cocode using "dta/industry_code.dta"
keep if _m==3
drop _m

keep if year == 2003

gen int_cov_ratio = sa_pbit/sa_interest_exp
drop if int_cov_ratio == .
bysort sa_finance1_cocode: egen avg_int_cov_ratio = mean(int_cov_ratio)
gen risky_firm1 = 0
replace risky_firm1 = 1 if avg_int_cov_ratio < 1

gen low_int_cov_ratio = 0
replace low_int_cov_ratio = 1 if int_cov_ratio < 1

bysort sa_finance1_cocode: egen risky_firm2 = max(low_int_cov_ratio)
bysort sa_finance1_cocode: egen risky_firm3 = min(low_int_cov_ratio)

ttest sa_secured_borrowings , by(risky_firm1)
ttest sa_short_term_bank_borrowings , by(risky_firm1)
ttest sa_long_term_bank_borrowings , by(risky_firm1)
ttest sa_quick_ratio , by(risky_firm1)
ttest sa_current_ratio , by(risky_firm1)
ttest sa_debt_equity , by(risky_firm1)

duplicates drop sa_finance1_cocode, force

keep sa_finance1_cocode co_industry_name sa_company_name risky_firm1 risky_firm2 risky_firm3 avg_int_cov_ratio

save "dta/firms_interest_coverage_placebo2.dta", replace

use "dta/MCA_data_psb_year_quarter_blank_placebo2.dta", replace
merge 1:1 creditorcode firmcode year quarter using "dta/MCA_data_psb_quarter_included_placebo2.dta"

bysort creditorcode firmcode: egen first_sanction_period = min(period)
bysort creditorcode firmcode: egen last_repayment_period = max(period_repayment)

replace total_loan_amount = 0 if _m==1
replace total_loan_given = 0 if _m==1

drop _m

gen period_new = 4*(year-2000) + quarter

rename firmcode sa_finance1_cocode

save "dta/MCA_data_psb_year_quarter_final_placebo2.dta", replace

use "dta/MCA_data_psb_year_quarter_final_placebo2.dta", replace

keep if period_new >= 1 & period_new <= 18
keep if total_loan_given != 0
duplicates drop sa_finance1_cocode creditorcode, force

keep sa_finance1_cocode creditorcode 

distinct sa_finance1_cocode
distinct creditorcode

save "dta/prev_yrs_loan_firms_quarterly_placebo2.dta", replace

use "dta/MCA_data_psb_year_quarter_final_placebo2.dta", replace

merge m:1 sa_finance1_cocode creditorcode using "dta/prev_yrs_loan_firms_quarterly_placebo2.dta"

keep if _m==3
drop _m

merge m:1 creditorcode using "dta/ceo_change_quarterly_placebo2.dta"

replace period_ceo = 10000 if _m==1

drop if _m==2 
drop _m

gen treat1 = 0
replace treat1 = 1 if period_new >= period_ceo

gen treat2 = 0
replace treat2 = 1 if period_new > period_ceo

bysort creditorcode: egen sum_treat1 = sum(treat1)

count if period_new >= 20 & period_new <= 25

merge m:1 sa_finance1_cocode using "dta/firms_interest_coverage_placebo2.dta"
keep if _m==3
drop _m

distinct sa_finance1_cocode if period_new >= 20 & period_new <= 25 & risky_firm1 == 1
distinct sa_finance1_cocode if period_new >= 20 & period_new <= 25 & risky_firm1 == 0

count if period_new >= 20 & period_new <= 25

merge m:1 sa_finance1_cocode using "dta/industry.dta"
drop if _m==2
drop _m

gen int_treat1_risky1 = treat1*risky_firm1
gen int_treat1_risky2 = treat1*risky_firm2
gen int_treat1_risky3 = treat1*risky_firm3
gen int_treat2_risky1 = treat2*risky_firm1
gen int_treat2_risky2 = treat2*risky_firm2
gen int_treat2_risky3 = treat2*risky_firm3

gen total_loan_given_dummy = 0
replace total_loan_given_dummy = 1 if total_loan_given > 0

egen firm_year = group(year sa_finance1_cocode)
egen firm_period = group(period_new  sa_finance1_cocode)
gen nic_prod_code_2 = substr(nic_prod_code,1,2)
gen nic_prod_code_3 = substr(nic_prod_code,1,3)

bysort creditorcode: egen first_period_ceo = min(period_new) if treat1 == 1
bysort creditorcode: egen min_first_period_ceo = min(first_period_ceo)

gen dist_from_ceo_change = period_new - min_first_period

gen dummy_pre1 = 0
replace dummy_pre1 = 1 if dist_from_ceo_change == -1
gen dummy_pre2 = 0
replace dummy_pre2 = 1 if dist_from_ceo_change == -2
gen dummy_pre3 = 0
replace dummy_pre3 = 1 if dist_from_ceo_change == -3
gen dummy_pre4 = 0
replace dummy_pre4 = 1 if dist_from_ceo_change == -4
gen dummy_pre5 = 0
replace dummy_pre5 = 1 if dist_from_ceo_change == -5
gen dummy_post = 0
replace dummy_post = 1 if dist_from_ceo_change >= 0 & dist_from_ceo_change != .

gen dummy_pre1_risky1 = dummy_pre1*risky_firm1
gen dummy_pre2_risky1 = dummy_pre2*risky_firm1
gen dummy_pre3_risky1 = dummy_pre3*risky_firm1
gen dummy_pre4_risky1 = dummy_pre4*risky_firm1
gen dummy_pre5_risky1 = dummy_pre5*risky_firm1
gen dummy_post_risky1 = dummy_post*risky_firm1

gen lg_loan = log(1+total_loan_amount)

summ total_loan_given if period_new >= 20 & period_new <= 25, d
summ total_loan_given_dummy if period_new >= 20 & period_new <= 25, d
summ total_loan_amount if period_new >= 20 & period_new <= 25, d

merge m:1 creditorcode year quarter using "dta/bank_info.dta"
drop if _m==2
drop _m

merge m:1 creditorcode year using "dta/capital_adequacy_ratio.dta"
drop if _m==2
drop _m

gen log_income = log(1 + siq_ntrm_total_inc)
gen log_pat = log(1 + siq_ntrm_after_tax_profit_loss_c )

gen log_income_year = log(1 + sa_total_income)
gen pat_income = sa_pat/sa_total_income
gen gnpa_close_loan_adv = sa_bd_gross_npa_closing_bal/sa_total_loans_and_advances

bysort creditorcode sa_finance1_cocode: egen min_period_new = min(period_new) if total_loan_given != 0
gen diff_min_period_new = period_new - min_period_new
replace diff_min_period_new = 0 if diff_min_period_new == . | diff_min_period_new < 0

save "dta/tables_placebo2.dta", replace

use "raw\LoansInMCA(2).dta", replace

keep if sanctionDate  >= td(01apr1999) & sanctionDate  <= td(30sep2003)
keep if modificationDate  >= td(01apr1999) & modificationDate  <= td(31mar2006)

rename (creditorCode firmCode) (creditorcode firmcode)

merge m:1 creditorcode using "dta/public_sector_banks1.dta"

keep if _m==3
drop _m

gen period = .

replace period = 1 if modificationDate >= td(01apr1999) & modificationDate <= td(30jun1999)
replace period = 2 if modificationDate >= td(01jul1999) & modificationDate <= td(30sep1999)
replace period = 3 if modificationDate >= td(01oct1999) & modificationDate <= td(31dec1999)
replace period = 4 if modificationDate >= td(01jan2000) & modificationDate <= td(31mar2000)
replace period = 5 if modificationDate >= td(01apr2000) & modificationDate <= td(30jun2000)
replace period = 6 if modificationDate >= td(01jul2000) & modificationDate <= td(30sep2000)
replace period = 7 if modificationDate >= td(01oct2000) & modificationDate <= td(31dec2000)
replace period = 8 if modificationDate >= td(01jan2001) & modificationDate <= td(31mar2001)
replace period = 9 if modificationDate >= td(01apr2001) & modificationDate <= td(30jun2001)
replace period = 10 if modificationDate >= td(01jul2001) & modificationDate <= td(30sep2001)
replace period = 11 if modificationDate >= td(01oct2001) & modificationDate <= td(31dec2001)
replace period = 12 if modificationDate >= td(01jan2002) & modificationDate <= td(31mar2002)
replace period = 13 if modificationDate >= td(01apr2002) & modificationDate <= td(30jun2002)
replace period = 14 if modificationDate >= td(01jul2002) & modificationDate <= td(30sep2002)
replace period = 15 if modificationDate >= td(01oct2002) & modificationDate <= td(31dec2002)
replace period = 16 if modificationDate >= td(01jan2003) & modificationDate <= td(31mar2003)
replace period = 17 if modificationDate >= td(01apr2003) & modificationDate <= td(30jun2003)
replace period = 18 if modificationDate >= td(01jul2003) & modificationDate <= td(30sep2003)
replace period = 19 if modificationDate >= td(01oct2003) & modificationDate <= td(31dec2003)
replace period = 20 if modificationDate >= td(01jan2004) & modificationDate <= td(31mar2004)
replace period = 21 if modificationDate >= td(01apr2004) & modificationDate <= td(30jun2004)
replace period = 22 if modificationDate >= td(01jul2004) & modificationDate <= td(30sep2004)
replace period = 23 if modificationDate >= td(01oct2004) & modificationDate <= td(31dec2004)
replace period = 24 if modificationDate >= td(01jan2005) & modificationDate <= td(31mar2005)
replace period = 25 if modificationDate >= td(01apr2005) & modificationDate <= td(30jun2005)
replace period = 26 if modificationDate >= td(01jul2005) & modificationDate <= td(30sep2005)
replace period = 27 if modificationDate >= td(01oct2005) & modificationDate <= td(31dec2005)
replace period = 28 if modificationDate >= td(01jan2006) & modificationDate <= td(31mar2006)
replace period = 29 if modificationDate >= td(01apr2006) & modificationDate <= td(30jun2006)
replace period = 30 if modificationDate >= td(01jul2006) & modificationDate <= td(30sep2006)
replace period = 31 if modificationDate >= td(01oct2006) & modificationDate <= td(31dec2006)
replace period = 32 if modificationDate >= td(01jan2007) & modificationDate <= td(31mar2007)
replace period = 33 if modificationDate >= td(01apr2007) & modificationDate <= td(30jun2007)
replace period = 34 if modificationDate >= td(01jul2007) & modificationDate <= td(30sep2007)
replace period = 35 if modificationDate >= td(01oct2007) & modificationDate <= td(31dec2007)
replace period = 36 if modificationDate >= td(01jan2008) & modificationDate <= td(31mar2008)

gen period_sanction = .

replace period_sanction = 1 if sanctionDate >= td(01apr1999) & sanctionDate <= td(30jun1999)
replace period_sanction = 2 if sanctionDate >= td(01jul1999) & sanctionDate <= td(30sep1999)
replace period_sanction = 3 if sanctionDate >= td(01oct1999) & sanctionDate <= td(31dec1999)
replace period_sanction = 4 if sanctionDate >= td(01jan2000) & sanctionDate <= td(31mar2000)
replace period_sanction = 5 if sanctionDate >= td(01apr2000) & sanctionDate <= td(30jun2000)
replace period_sanction = 6 if sanctionDate >= td(01jul2000) & sanctionDate <= td(30sep2000)
replace period_sanction = 7 if sanctionDate >= td(01oct2000) & sanctionDate <= td(31dec2000)
replace period_sanction = 8 if sanctionDate >= td(01jan2001) & sanctionDate <= td(31mar2001)
replace period_sanction = 9 if sanctionDate >= td(01apr2001) & sanctionDate <= td(30jun2001)
replace period_sanction = 10 if sanctionDate >= td(01jul2001) & sanctionDate <= td(30sep2001)
replace period_sanction = 11 if sanctionDate >= td(01oct2001) & sanctionDate <= td(31dec2001)
replace period_sanction = 12 if sanctionDate >= td(01jan2002) & sanctionDate <= td(31mar2002)
replace period_sanction = 13 if sanctionDate >= td(01apr2002) & sanctionDate <= td(30jun2002)
replace period_sanction = 14 if sanctionDate >= td(01jul2002) & sanctionDate <= td(30sep2002)
replace period_sanction = 15 if sanctionDate >= td(01oct2002) & sanctionDate <= td(31dec2002)
replace period_sanction = 16 if sanctionDate >= td(01jan2003) & sanctionDate <= td(31mar2003)
replace period_sanction = 17 if sanctionDate >= td(01apr2003) & sanctionDate <= td(30jun2003)
replace period_sanction = 18 if sanctionDate >= td(01jul2003) & sanctionDate <= td(30sep2003)
replace period_sanction = 19 if sanctionDate >= td(01oct2003) & sanctionDate <= td(31dec2003)
replace period_sanction = 20 if sanctionDate >= td(01jan2004) & sanctionDate <= td(31mar2004)
replace period_sanction = 21 if sanctionDate >= td(01apr2004) & sanctionDate <= td(30jun2004)
replace period_sanction = 22 if sanctionDate >= td(01jul2004) & sanctionDate <= td(30sep2004)
replace period_sanction = 23 if sanctionDate >= td(01oct2004) & sanctionDate <= td(31dec2004)
replace period_sanction = 24 if sanctionDate >= td(01jan2005) & sanctionDate <= td(31mar2005)
replace period_sanction = 25 if sanctionDate >= td(01apr2005) & sanctionDate <= td(30jun2005)
replace period_sanction = 26 if sanctionDate >= td(01jul2005) & sanctionDate <= td(30sep2005)
replace period_sanction = 27 if sanctionDate >= td(01oct2005) & sanctionDate <= td(31dec2005)
replace period_sanction = 28 if sanctionDate >= td(01jan2006) & sanctionDate <= td(31mar2006)
replace period_sanction = 29 if sanctionDate >= td(01apr2006) & sanctionDate <= td(30jun2006)
replace period_sanction = 30 if sanctionDate >= td(01jul2006) & sanctionDate <= td(30sep2006)
replace period_sanction = 31 if sanctionDate >= td(01oct2006) & sanctionDate <= td(31dec2006)
replace period_sanction = 32 if sanctionDate >= td(01jan2007) & sanctionDate <= td(31mar2007)
replace period_sanction = 33 if sanctionDate >= td(01apr2007) & sanctionDate <= td(30jun2007)
replace period_sanction = 34 if sanctionDate >= td(01jul2007) & sanctionDate <= td(30sep2007)
replace period_sanction = 35 if sanctionDate >= td(01oct2007) & sanctionDate <= td(31dec2007)
replace period_sanction = 36 if sanctionDate >= td(01jan2008) & sanctionDate <= td(31mar2008)

gen year = int((period-1)/4)
replace year = year + 2002
gen quarter = mod(period,4)
replace quarter = 4 if quarter == 0

bysort creditorcode firmcode year quarter: gen num_of_modifications = _N

gen same_period_modification = 0
replace same_period_modification = 1 if period == period_sanction

bysort creditorcode firmcode year quarter: egen num_same_modifications = sum(same_period_modification)
bysort creditorcode firmcode year quarter: egen loan_amount_modified = sum(loanAmount)

gen num_of_modifications2 = num_of_modifications - num_same_modifications

duplicates drop creditorcode firmcode year quarter, force

rename firmcode sa_finance1_cocode

keep creditorcode sa_finance1_cocode year quarter period num_of_modifications num_of_modifications2 loanAmount loan_amount_modified

save "dta/restructuring_psb_final_placebo2.dta", replace

********************************************************* Data Creation - Table A5 (Placebo - Panel B) (End) ************************************



****************************************************************************************************************************************************
/* Table A5 - Placebo (Panel B) */
****************************************************************************************************************************************************

use "dta/tables_placebo2.dta", replace

drop int_treat1_risky1

replace treat1 = 0 if creditorcode == 236337

gen int_treat1_risky1 = treat1*risky_firm1

gen firmCode = sa_finance1_cocode
gen creditorCode = creditorcode
merge m:1 firmCode creditorCode year using "dta/FirmBankYearControls_2018 - Copy.dta"
drop if _m == 2
drop _m

merge m:1 sa_finance1_cocode year using "dta/firm_year_controls.dta"
drop if _m == 2
drop _m

gen lag_amount = exp(lag_lg_amount)
gen lag_amount_assets = lag_amount/(sa_total_assets*1000000)

merge m:1 sa_finance1_cocode creditorcode year quarter using "dta/restructuring_psb_final_placebo2.dta"

replace num_of_modifications = 0 if _m==1
replace num_of_modifications2 = 0 if _m==1

drop if _m==2
drop _m

gen num_of_modifications_dummy = 0
replace num_of_modifications_dummy = 1 if num_of_modifications > 0

gen num_of_modifications_dummy2 = 0
replace num_of_modifications_dummy2 = 1 if num_of_modifications2 > 0

// Bank*Time groups
egen bank_quarter = group(creditorcode period_new)

eststo clear
label var total_loan_given_dummy "Loan Indicator"
label var lg_loan "Log(Loan Amount)"
label var int_treat1_risky1 "CEO change x Low-quality firm"
label var treat1 "CEO change"
label var risky_firm1 "Low-quality firm"


reghdfe total_loan_given_dummy int_treat1_risky1 treat1 risky_firm1 avg_loan_duration loan_completed_5 lag_amount_assets if period_new >= 20 & period_new <= 25, absorb(creditorcode  firm_year) cluster(nic_prod_code)
outreg2 using "Referee/tables/placebo_reg2.xls", replace label ctitle (Loan Dummy) dec(4) se coefastr bracket nocons addtext (Controls, Yes, Bank Fixed Effects, Yes, Bank x Quarter Fixed Effects, No, Firm x Year Fixed Effects, Yes, Bank-Firm Fixed Effects, No)

reghdfe total_loan_given_dummy int_treat1_risky1 treat1 risky_firm1 avg_loan_duration loan_completed_5 lag_amount_assets if period_new >= 20 & period_new <= 25, absorb(bank_quarter firm_year) cluster(nic_prod_code)
outreg2 using "Referee/tables/placebo_reg2.xls", append label ctitle (Loan Dummy) dec(4) se coefastr bracket nocons addtext (Controls, Yes, Bank Fixed Effects, No, Bank x Quarter Fixed Effects, Yes, Firm x Year Fixed Effects, Yes, Bank-Firm Fixed Effects, No)

reghdfe total_loan_given_dummy int_treat1_risky1 treat1 risky_firm1 avg_loan_duration loan_completed_5 lag_amount_assets if period_new >= 20 & period_new <= 25, absorb(bank_quarter bank_firm  firm_year) cluster(nic_prod_code)
outreg2 using "Referee/tables/placebo_reg2.xls", append label ctitle (Loan Dummy) dec(4) se coefastr bracket nocons addtext (Controls, Yes, Bank Fixed Effects, No, Bank x Quarter Fixed Effects, Yes, Firm x Year Fixed Effects, Yes, Bank-Firm Fixed Effects, Yes)

reghdfe num_of_modifications_dummy  int_treat1_risky1 treat1 risky_firm1 avg_loan_duration loan_completed_5 lag_amount_assets if period_new >= 20 & period_new <= 25, absorb(creditorcode  firm_year) cluster(nic_prod_code)
outreg2 using "Referee/tables/placebo_reg2.xls", append label ctitle (Restructuring) dec(4) se coefastr bracket nocons addtext (Controls, Yes, Bank Fixed Effects, Yes, Bank x Quarter Fixed Effects, No, Firm x Year Fixed Effects, Yes, Bank-Firm Fixed Effects, No)

reghdfe num_of_modifications_dummy  int_treat1_risky1 treat1 risky_firm1 avg_loan_duration loan_completed_5 lag_amount_assets if period_new >= 20 & period_new <= 25, absorb(bank_quarter firm_year) cluster(nic_prod_code)
outreg2 using "Referee/tables/placebo_reg2.xls", append label ctitle (Restructuring) dec(4) se coefastr bracket nocons addtext (Controls, Yes, Bank Fixed Effects, No, Bank x Quarter Fixed Effects, Yes, Firm x Year Fixed Effects, Yes, Bank-Firm Fixed Effects, No)

reghdfe num_of_modifications_dummy  int_treat1_risky1 treat1 risky_firm1 avg_loan_duration loan_completed_5 lag_amount_assets if period_new >= 20 & period_new <= 25, absorb(bank_quarter bank_firm  firm_year) cluster(nic_prod_code)
outreg2 using "Referee/tables/placebo_reg2.xls", append label ctitle (Restructuring) dec(4) se coefastr bracket nocons addtext (Controls, Yes, Bank Fixed Effects, No, Bank x Quarter Fixed Effects, Yes, Firm x Year Fixed Effects, Yes, Bank-Firm Fixed Effects, Yes)



********************************************************* Data Creation - Table A6 (Covid - External Validation) (Begin) *******************************

import delimited "raw\firms_interest_coverage.txt", delimiter("|") clear

keep sa_finance1_cocode sa_finance1_year sa_company_name sa_pbit sa_interest_exp

destring sa_pbit sa_interest_exp, force replace

tostring sa_finance1_year, force replace
gen day = substr(sa_finance1_year,7,2)
gen month = substr(sa_finance1_year,5,2)
gen year = substr(sa_finance1_year,1,4)

destring day-year, force replace

duplicates tag sa_finance1_cocode year, gen(x)
bysort sa_finance1_cocode: gen march = 1 if x>0 & month == 3
bysort sa_finance1_cocode: egen max_march = max(march) if x>0
drop if x>0 & max_march == 1 & march != 1
sort sa_finance1_cocode year month
bysort sa_finance1_cocode year: gen dup_n = _n if x > 0 & max_march != 1 
drop if dup_n > 1 & dup_n != .
sort sa_finance1_cocode year month day
bysort sa_finance1_cocode year: egen march_dup = sum(march) if x > 0 & max_march == 1 
bysort sa_finance1_cocode year: gen march_dup_n = _n if march_dup > 1 & march_dup != .
bysort sa_finance1_cocode year: gen march_dup_total_n = _N if march_dup > 1 & march_dup != .
drop if march_dup_n < march_dup_total_n & march_dup_n != .

drop x-march_dup_total_n

merge m:1 sa_finance1_cocode using "dta/industry_code.dta"
keep if _m==3
drop _m

merge m:1 sa_finance1_cocode using "dta/age_of_firms.dta"
drop if _m==2
drop _m

gen age = year - incorporation_year

tsset sa_finance1_cocode year, yearly

gen int_cov_ratio = sa_pbit/sa_interest_exp
drop if int_cov_ratio == .

gen risky_firm_zombie = 0
replace risky_firm_zombie = 1 if (int_cov_ratio < 1) & (L1.int_cov_ratio < 1) & (L2.int_cov_ratio < 1) & (age >= 10)

keep if year == 2019 | year == 2018

bysort sa_finance1_cocode: egen avg_int_cov_ratio = mean(int_cov_ratio)
gen risky_firm1 = 0
replace risky_firm1 = 1 if avg_int_cov_ratio < 1

gen low_int_cov_ratio = 0
replace low_int_cov_ratio = 1 if int_cov_ratio < 1

bysort sa_finance1_cocode: egen risky_firm2 = max(low_int_cov_ratio)
bysort sa_finance1_cocode: egen risky_firm3 = min(low_int_cov_ratio)

duplicates drop sa_finance1_cocode, force

keep sa_finance1_cocode co_industry_name sa_company_name risky_firm1 risky_firm2 risky_firm3 avg_int_cov_ratio risky_firm_zombie

save "dta_covid/firms_interest_coverage.dta", replace

****************************************************************************************************************************************************

****************************************************************************************************************************************************

use "raw\CEO_Tenure_covid.dta", clear

rename (creditorCode CEO sa_company_name) (creditorcode ceo psbname)

keep if ceo >= td(01jan2020) & ceo <= td(30sep2020)

bysort psbname: egen min_ceo = min(ceo)

format min_ceo %td

gen period_ceo = .
replace period_ceo = 1 if min_ceo >= td(01apr2013) & min_ceo <= td(30jun2013)
replace period_ceo = 2 if min_ceo >= td(01jul2013) & min_ceo <= td(30sep2013)
replace period_ceo = 3 if min_ceo >= td(01oct2013) & min_ceo <= td(31dec2013)
replace period_ceo = 4 if min_ceo >= td(01jan2014) & min_ceo <= td(31mar2014)
replace period_ceo = 5 if min_ceo >= td(01apr2014) & min_ceo <= td(30jun2014)
replace period_ceo = 6 if min_ceo >= td(01jul2014) & min_ceo <= td(30sep2014)
replace period_ceo = 7 if min_ceo >= td(01oct2014) & min_ceo <= td(31dec2014)
replace period_ceo = 8 if min_ceo >= td(01jan2015) & min_ceo <= td(31mar2015)
replace period_ceo = 9 if min_ceo >= td(01apr2015) & min_ceo <= td(30jun2015)
replace period_ceo = 10 if min_ceo >= td(01jul2015) & min_ceo <= td(30sep2015)
replace period_ceo = 11 if min_ceo >= td(01oct2015) & min_ceo <= td(31dec2015)
replace period_ceo = 12 if min_ceo >= td(01jan2016) & min_ceo <= td(31mar2016)
replace period_ceo = 13 if min_ceo >= td(01apr2016) & min_ceo <= td(30jun2016)
replace period_ceo = 14 if min_ceo >= td(01jul2016) & min_ceo <= td(30sep2016)
replace period_ceo = 15 if min_ceo >= td(01oct2016) & min_ceo <= td(31dec2016)
replace period_ceo = 16 if min_ceo >= td(01jan2017) & min_ceo <= td(31mar2017)
replace period_ceo = 17 if min_ceo >= td(01apr2017) & min_ceo <= td(30jun2017)
replace period_ceo = 18 if min_ceo >= td(01jul2017) & min_ceo <= td(30sep2017)
replace period_ceo = 19 if min_ceo >= td(01oct2017) & min_ceo <= td(31dec2017)
replace period_ceo = 20 if min_ceo >= td(01jan2018) & min_ceo <= td(31mar2018)
replace period_ceo = 21 if min_ceo >= td(01apr2018) & min_ceo <= td(30jun2018)
replace period_ceo = 22 if min_ceo >= td(01jul2018) & min_ceo <= td(30sep2018)
replace period_ceo = 23 if min_ceo >= td(01oct2018) & min_ceo <= td(31dec2018)
replace period_ceo = 24 if min_ceo >= td(01jan2019) & min_ceo <= td(31mar2019)
replace period_ceo = 25 if min_ceo >= td(01apr2019) & min_ceo <= td(30jun2019)
replace period_ceo = 26 if min_ceo >= td(01jul2019) & min_ceo <= td(30sep2019)
replace period_ceo = 27 if min_ceo >= td(01oct2019) & min_ceo <= td(31dec2019)
replace period_ceo = 28 if min_ceo >= td(01jan2020) & min_ceo <= td(31mar2020)
replace period_ceo = 29 if min_ceo >= td(01apr2020) & min_ceo <= td(30jun2020)
replace period_ceo = 30 if min_ceo >= td(01jul2020) & min_ceo <= td(30sep2020)
replace period_ceo = 31 if min_ceo >= td(01oct2020) & min_ceo <= td(31dec2020)
replace period_ceo = 32 if min_ceo >= td(01jan2021) & min_ceo <= td(31mar2021)
replace period_ceo = 33 if min_ceo >= td(01apr2021) & min_ceo <= td(30jun2021)
replace period_ceo = 34 if min_ceo >= td(01jul2021) & min_ceo <= td(30sep2021)
replace period_ceo = 35 if min_ceo >= td(01oct2021) & min_ceo <= td(31dec2021)
replace period_ceo = 36 if min_ceo >= td(01jan2022) & min_ceo <= td(31mar2022)

save "dta_covid/ceo_change_quarterly.dta", replace

use "dta/public_sector_banks1.dta", replace

drop if creditorcode == 100283

save "dta/public_sector_banks2.dta", replace

use "dta/MCA_data_with_dates.dta", replace

keep if sanctionDate >= td(01apr2013) & sanctionDate <= td(31mar2022)

duplicates drop creditorCode firmCode, force

rename (creditorCode firmCode) (creditorcode firmcode)

merge m:1 creditorcode using "dta/public_sector_banks2.dta"

keep if _m==3
drop _m

keep creditorName creditorcode firmcode firmName psb_name

save "dta_covid/MCA_data_psb.dta", replace

****************************************************************************************************************************************************

****************************************************************************************************************************************************

use "dta_covid/MCA_data_psb.dta", replace
append using "dta_covid/MCA_data_psb.dta"
append using "dta_covid/MCA_data_psb.dta"
append using "dta_covid/MCA_data_psb.dta"

bysort creditorcode firmcode: gen quarter = _n

save "dta_covid/MCA_data_psb_only_quarter.dta", replace

****************************************************************************************************************************************************

****************************************************************************************************************************************************

use "dta_covid/MCA_data_psb_only_quarter.dta", replace
append using "dta_covid/MCA_data_psb_only_quarter.dta"
append using "dta_covid/MCA_data_psb_only_quarter.dta"
append using "dta_covid/MCA_data_psb_only_quarter.dta"
append using "dta_covid/MCA_data_psb_only_quarter.dta"
append using "dta_covid/MCA_data_psb_only_quarter.dta"
append using "dta_covid/MCA_data_psb_only_quarter.dta"
append using "dta_covid/MCA_data_psb_only_quarter.dta"
append using "dta_covid/MCA_data_psb_only_quarter.dta"

bysort creditorcode firmcode quarter: gen year = _n

replace year = year + 2013

save "dta_covid/MCA_data_psb_year_quarter_blank.dta", replace

****************************************************************************************************************************************************

****************************************************************************************************************************************************

use "dta/MCA_data_with_dates.dta", replace

keep if sanctionDate >= td(01apr2013) & sanctionDate <= td(31mar2022)

rename (creditorCode firmCode) (creditorcode firmcode)

merge m:1 creditorcode using "dta/public_sector_banks1.dta"

keep if _m==3
drop _m

gen period = .

replace period = 1 if sanctionDate >= td(01apr2013) & sanctionDate <= td(30jun2013)
replace period = 2 if sanctionDate >= td(01jul2013) & sanctionDate <= td(30sep2013)
replace period = 3 if sanctionDate >= td(01oct2013) & sanctionDate <= td(31dec2013)
replace period = 4 if sanctionDate >= td(01jan2014) & sanctionDate <= td(31mar2014)
replace period = 5 if sanctionDate >= td(01apr2014) & sanctionDate <= td(30jun2014)
replace period = 6 if sanctionDate >= td(01jul2014) & sanctionDate <= td(30sep2014)
replace period = 7 if sanctionDate >= td(01oct2014) & sanctionDate <= td(31dec2014)
replace period = 8 if sanctionDate >= td(01jan2015) & sanctionDate <= td(31mar2015)
replace period = 9 if sanctionDate >= td(01apr2015) & sanctionDate <= td(30jun2015)
replace period = 10 if sanctionDate >= td(01jul2015) & sanctionDate <= td(30sep2015)
replace period = 11 if sanctionDate >= td(01oct2015) & sanctionDate <= td(31dec2015)
replace period = 12 if sanctionDate >= td(01jan2016) & sanctionDate <= td(31mar2016)
replace period = 13 if sanctionDate >= td(01apr2016) & sanctionDate <= td(30jun2016)
replace period = 14 if sanctionDate >= td(01jul2016) & sanctionDate <= td(30sep2016)
replace period = 15 if sanctionDate >= td(01oct2016) & sanctionDate <= td(31dec2016)
replace period = 16 if sanctionDate >= td(01jan2017) & sanctionDate <= td(31mar2017)
replace period = 17 if sanctionDate >= td(01apr2017) & sanctionDate <= td(30jun2017)
replace period = 18 if sanctionDate >= td(01jul2017) & sanctionDate <= td(30sep2017)
replace period = 19 if sanctionDate >= td(01oct2017) & sanctionDate <= td(31dec2017)
replace period = 20 if sanctionDate >= td(01jan2018) & sanctionDate <= td(31mar2018)
replace period = 21 if sanctionDate >= td(01apr2018) & sanctionDate <= td(30jun2018)
replace period = 22 if sanctionDate >= td(01jul2018) & sanctionDate <= td(30sep2018)
replace period = 23 if sanctionDate >= td(01oct2018) & sanctionDate <= td(31dec2018)
replace period = 24 if sanctionDate >= td(01jan2019) & sanctionDate <= td(31mar2019)
replace period = 25 if sanctionDate >= td(01apr2019) & sanctionDate <= td(30jun2019)
replace period = 26 if sanctionDate >= td(01jul2019) & sanctionDate <= td(30sep2019)
replace period = 27 if sanctionDate >= td(01oct2019) & sanctionDate <= td(31dec2019)
replace period = 28 if sanctionDate >= td(01jan2020) & sanctionDate <= td(31mar2020)
replace period = 29 if sanctionDate >= td(01apr2020) & sanctionDate <= td(30jun2020)
replace period = 30 if sanctionDate >= td(01jul2020) & sanctionDate <= td(30sep2020)
replace period = 31 if sanctionDate >= td(01oct2020) & sanctionDate <= td(31dec2020)
replace period = 32 if sanctionDate >= td(01jan2021) & sanctionDate <= td(31mar2021)
replace period = 33 if sanctionDate >= td(01apr2021) & sanctionDate <= td(30jun2021)
replace period = 34 if sanctionDate >= td(01jul2021) & sanctionDate <= td(30sep2021)
replace period = 35 if sanctionDate >= td(01oct2021) & sanctionDate <= td(31dec2021)
replace period = 36 if sanctionDate >= td(01jan2022) & sanctionDate <= td(31mar2022)

gen year = int((period-1)/4)
replace year = year + 2014
gen quarter = mod(period,4)
replace quarter = 4 if quarter == 0

gen zero_loan_amount = 0
replace zero_loan_amount = 1 if loanAmount == 0

bysort creditorcode firmcode year quarter: egen total_loan_amount = sum(loanAmount)
bysort creditorcode firmcode year quarter: egen total_loan_given = sum(psb)

duplicates drop creditorcode firmcode year quarter, force

save "dta_covid/MCA_data_psb_quarter_included.dta", replace

****************************************************************************************************************************************************
/* Creating restructuring data (modifications) */
****************************************************************************************************************************************************

use "raw\LoansInMCA(2).dta", replace

keep if sanctionDate  >= td(01apr2013) & sanctionDate  <= td(31dec2019)
keep if modificationDate  >= td(01apr2013) & modificationDate  <= td(31mar2022)

rename (creditorCode firmCode) (creditorcode firmcode)

merge m:1 creditorcode using "dta/public_sector_banks1.dta"

keep if _m==3
drop _m

gen period = .

replace period = 1 if modificationDate >= td(01apr2013) & modificationDate <= td(30jun2013)
replace period = 2 if modificationDate >= td(01jul2013) & modificationDate <= td(30sep2013)
replace period = 3 if modificationDate >= td(01oct2013) & modificationDate <= td(31dec2013)
replace period = 4 if modificationDate >= td(01jan2014) & modificationDate <= td(31mar2014)
replace period = 5 if modificationDate >= td(01apr2014) & modificationDate <= td(30jun2014)
replace period = 6 if modificationDate >= td(01jul2014) & modificationDate <= td(30sep2014)
replace period = 7 if modificationDate >= td(01oct2014) & modificationDate <= td(31dec2014)
replace period = 8 if modificationDate >= td(01jan2015) & modificationDate <= td(31mar2015)
replace period = 9 if modificationDate >= td(01apr2015) & modificationDate <= td(30jun2015)
replace period = 10 if modificationDate >= td(01jul2015) & modificationDate <= td(30sep2015)
replace period = 11 if modificationDate >= td(01oct2015) & modificationDate <= td(31dec2015)
replace period = 12 if modificationDate >= td(01jan2016) & modificationDate <= td(31mar2016)
replace period = 13 if modificationDate >= td(01apr2016) & modificationDate <= td(30jun2016)
replace period = 14 if modificationDate >= td(01jul2016) & modificationDate <= td(30sep2016)
replace period = 15 if modificationDate >= td(01oct2016) & modificationDate <= td(31dec2016)
replace period = 16 if modificationDate >= td(01jan2017) & modificationDate <= td(31mar2017)
replace period = 17 if modificationDate >= td(01apr2017) & modificationDate <= td(30jun2017)
replace period = 18 if modificationDate >= td(01jul2017) & modificationDate <= td(30sep2017)
replace period = 19 if modificationDate >= td(01oct2017) & modificationDate <= td(31dec2017)
replace period = 20 if modificationDate >= td(01jan2018) & modificationDate <= td(31mar2018)
replace period = 21 if modificationDate >= td(01apr2018) & modificationDate <= td(30jun2018)
replace period = 22 if modificationDate >= td(01jul2018) & modificationDate <= td(30sep2018)
replace period = 23 if modificationDate >= td(01oct2018) & modificationDate <= td(31dec2018)
replace period = 24 if modificationDate >= td(01jan2019) & modificationDate <= td(31mar2019)
replace period = 25 if modificationDate >= td(01apr2019) & modificationDate <= td(30jun2019)
replace period = 26 if modificationDate >= td(01jul2019) & modificationDate <= td(30sep2019)
replace period = 27 if modificationDate >= td(01oct2019) & modificationDate <= td(31dec2019)
replace period = 28 if modificationDate >= td(01jan2020) & modificationDate <= td(31mar2020)
replace period = 29 if modificationDate >= td(01apr2020) & modificationDate <= td(30jun2020)
replace period = 30 if modificationDate >= td(01jul2020) & modificationDate <= td(30sep2020)
replace period = 31 if modificationDate >= td(01oct2020) & modificationDate <= td(31dec2020)
replace period = 32 if modificationDate >= td(01jan2021) & modificationDate <= td(31mar2021)
replace period = 33 if modificationDate >= td(01apr2021) & modificationDate <= td(30jun2021)
replace period = 34 if modificationDate >= td(01jul2021) & modificationDate <= td(30sep2021)
replace period = 35 if modificationDate >= td(01oct2021) & modificationDate <= td(31dec2021)
replace period = 36 if modificationDate >= td(01jan2022) & modificationDate <= td(31mar2022)



gen period_sanction = .

replace period_sanction = 1 if sanctionDate >= td(01apr2013) & sanctionDate <= td(30jun2013)
replace period_sanction = 2 if sanctionDate >= td(01jul2013) & sanctionDate <= td(30sep2013)
replace period_sanction = 3 if sanctionDate >= td(01oct2013) & sanctionDate <= td(31dec2013)
replace period_sanction = 4 if sanctionDate >= td(01jan2014) & sanctionDate <= td(31mar2014)
replace period_sanction = 5 if sanctionDate >= td(01apr2014) & sanctionDate <= td(30jun2014)
replace period_sanction = 6 if sanctionDate >= td(01jul2014) & sanctionDate <= td(30sep2014)
replace period_sanction = 7 if sanctionDate >= td(01oct2014) & sanctionDate <= td(31dec2014)
replace period_sanction = 8 if sanctionDate >= td(01jan2015) & sanctionDate <= td(31mar2015)
replace period_sanction = 9 if sanctionDate >= td(01apr2015) & sanctionDate <= td(30jun2015)
replace period_sanction = 10 if sanctionDate >= td(01jul2015) & sanctionDate <= td(30sep2015)
replace period_sanction = 11 if sanctionDate >= td(01oct2015) & sanctionDate <= td(31dec2015)
replace period_sanction = 12 if sanctionDate >= td(01jan2016) & sanctionDate <= td(31mar2016)
replace period_sanction = 13 if sanctionDate >= td(01apr2016) & sanctionDate <= td(30jun2016)
replace period_sanction = 14 if sanctionDate >= td(01jul2016) & sanctionDate <= td(30sep2016)
replace period_sanction = 15 if sanctionDate >= td(01oct2016) & sanctionDate <= td(31dec2016)
replace period_sanction = 16 if sanctionDate >= td(01jan2017) & sanctionDate <= td(31mar2017)
replace period_sanction = 17 if sanctionDate >= td(01apr2017) & sanctionDate <= td(30jun2017)
replace period_sanction = 18 if sanctionDate >= td(01jul2017) & sanctionDate <= td(30sep2017)
replace period_sanction = 19 if sanctionDate >= td(01oct2017) & sanctionDate <= td(31dec2017)
replace period_sanction = 20 if sanctionDate >= td(01jan2018) & sanctionDate <= td(31mar2018)
replace period_sanction = 21 if sanctionDate >= td(01apr2018) & sanctionDate <= td(30jun2018)
replace period_sanction = 22 if sanctionDate >= td(01jul2018) & sanctionDate <= td(30sep2018)
replace period_sanction = 23 if sanctionDate >= td(01oct2018) & sanctionDate <= td(31dec2018)
replace period_sanction = 24 if sanctionDate >= td(01jan2019) & sanctionDate <= td(31mar2019)
replace period_sanction = 25 if sanctionDate >= td(01apr2019) & sanctionDate <= td(30jun2019)
replace period_sanction = 26 if sanctionDate >= td(01jul2019) & sanctionDate <= td(30sep2019)
replace period_sanction = 27 if sanctionDate >= td(01oct2019) & sanctionDate <= td(31dec2019)
replace period_sanction = 28 if sanctionDate >= td(01jan2020) & sanctionDate <= td(31mar2020)
replace period_sanction = 29 if sanctionDate >= td(01apr2020) & sanctionDate <= td(30jun2020)
replace period_sanction = 30 if sanctionDate >= td(01jul2020) & sanctionDate <= td(30sep2020)
replace period_sanction = 31 if sanctionDate >= td(01oct2020) & sanctionDate <= td(31dec2020)
replace period_sanction = 32 if sanctionDate >= td(01jan2021) & sanctionDate <= td(31mar2021)
replace period_sanction = 33 if sanctionDate >= td(01apr2021) & sanctionDate <= td(30jun2021)
replace period_sanction = 34 if sanctionDate >= td(01jul2021) & sanctionDate <= td(30sep2021)
replace period_sanction = 35 if sanctionDate >= td(01oct2021) & sanctionDate <= td(31dec2021)
replace period_sanction = 36 if sanctionDate >= td(01jan2022) & sanctionDate <= td(31mar2022)

gen year = int((period-1)/4)
replace year = year + 2014
gen quarter = mod(period,4)
replace quarter = 4 if quarter == 0

bysort creditorcode firmcode year quarter: gen num_of_modifications = _N

gen same_period_modification = 0
replace same_period_modification = 1 if period == period_sanction

bysort creditorcode firmcode year quarter: egen num_same_modifications = sum(same_period_modification)

gen num_of_modifications2 = num_of_modifications - num_same_modifications

duplicates drop creditorcode firmcode year quarter, force

rename firmcode sa_finance1_cocode

keep creditorcode sa_finance1_cocode year quarter period num_of_modifications num_of_modifications2 loanAmount

save "dta_covid/restructuring_psb_final.dta", replace

****************************************************************************************************************************************************

****************************************************************************************************************************************************

use "dta_covid/MCA_data_psb_year_quarter_blank.dta", replace
merge 1:1 creditorcode firmcode year quarter using "dta_covid/MCA_data_psb_quarter_included.dta"

replace total_loan_amount = 0 if _m==1
replace total_loan_given = 0 if _m==1

drop _m

gen period_new = 4*(year-2014) + quarter

rename firmcode sa_finance1_cocode

save "dta_covid/MCA_data_psb_year_quarter_final.dta", replace

****************************************************************************************************************************************************

****************************************************************************************************************************************************

use "dta_covid/MCA_data_psb_year_quarter_final.dta", replace

keep if period_new >= 7 & period_new <= 25
keep if total_loan_given != 0
duplicates drop sa_finance1_cocode creditorcode, force

keep sa_finance1_cocode creditorcode

save "dta_covid/prev_yrs_loan_firms_quarterly.dta", replace

****************************************************************************************************************************************************

****************************************************************************************************************************************************

use "dta_covid/MCA_data_psb_year_quarter_final.dta", replace

merge m:1 sa_finance1_cocode creditorcode using "dta_covid/prev_yrs_loan_firms_quarterly.dta"

keep if _m==3
drop _m

merge m:1 creditorcode using "dta_covid/ceo_change_quarterly.dta"

replace period_ceo = 10000 if _m==1

drop if _m==2 
drop _m

gen treat1 = 0
replace treat1 = 1 if period_new >= period_ceo

gen treat2 = 0
replace treat2 = 1 if period_new > period_ceo

bysort creditorcode: egen sum_treat1 = sum(treat1)

merge m:1 sa_finance1_cocode creditorcode year quarter using "dta_covid/restructuring_psb_final.dta"

replace num_of_modifications = 0 if _m==1
replace num_of_modifications2 = 0 if _m==1

drop if _m==2
drop _m

merge m:1 sa_finance1_cocode using "dta_covid/firms_interest_coverage.dta"
keep if _m==3
drop _m

merge m:1 sa_finance1_cocode using "dta/industry.dta"
drop _m

gen num_of_modifications_dummy = 0
replace num_of_modifications_dummy = 1 if num_of_modifications > 0

gen num_of_modifications_dummy2 = 0
replace num_of_modifications_dummy2 = 1 if num_of_modifications2 > 0

gen int_treat1_risky1 = treat1*risky_firm1
gen int_treat1_risky2 = treat1*risky_firm2
gen int_treat1_risky3 = treat1*risky_firm3
gen int_treat2_risky1 = treat2*risky_firm1
gen int_treat2_risky2 = treat2*risky_firm2
gen int_treat2_risky3 = treat2*risky_firm3

gen total_loan_given_dummy = 0
replace total_loan_given_dummy = 1 if total_loan_given > 0

egen firm_year = group(year sa_finance1_cocode)
egen firm_period = group(period_new  sa_finance1_cocode)
gen nic_prod_code_2 = substr(nic_prod_code,1,2)
gen nic_prod_code_3 = substr(nic_prod_code,1,3)

bysort creditorcode: egen first_period_ceo = min(period_new) if treat1 == 1
bysort creditorcode: egen min_first_period_ceo = min(first_period_ceo)

gen dist_from_ceo_change = period_new - min_first_period

gen dummy_pre1 = 0
replace dummy_pre1 = 1 if dist_from_ceo_change == -1
gen dummy_pre2 = 0
replace dummy_pre2 = 1 if dist_from_ceo_change == -2
gen dummy_pre3 = 0
replace dummy_pre3 = 1 if dist_from_ceo_change == -3
gen dummy_pre4 = 0
replace dummy_pre4 = 1 if dist_from_ceo_change == -4
gen dummy_pre5 = 0
replace dummy_pre5 = 1 if dist_from_ceo_change == -5
gen dummy_post = 0
replace dummy_post = 1 if dist_from_ceo_change >= 0 & dist_from_ceo_change != .

gen dummy_pre1_risky1 = dummy_pre1*risky_firm1
gen dummy_pre2_risky1 = dummy_pre2*risky_firm1
gen dummy_pre3_risky1 = dummy_pre3*risky_firm1
gen dummy_pre4_risky1 = dummy_pre4*risky_firm1
gen dummy_pre5_risky1 = dummy_pre5*risky_firm1
gen dummy_post_risky1 = dummy_post*risky_firm1

gen lg_loan = log(1+total_loan_amount)

merge m:1 creditorcode year quarter using "dta/bank_info.dta"
drop if _m==2
drop _m

merge m:1 creditorcode year using "dta/capital_adequacy_ratio.dta"
drop if _m==2
drop _m

gen log_income = log(1 + siq_ntrm_total_inc)
gen log_pat = log(1 + siq_ntrm_after_tax_profit_loss_c )

gen log_income_year = log(1 + sa_total_income)
gen log_pat_year = log(1 + sa_pat )
gen log_pbt_year = log(1 + sa_pbt)
gen log_pbit_year = log(1 + sa_pbit )
gen log_gnpa_open = log(1 + sa_bd_gross_npa_opening_bal)
gen log_gnpa_close = log(1 + sa_bd_gross_npa_closing_bal)
gen log_nnpa_open = log(1 + sa_bd_net_npa_opening_bal)
gen log_nnpa_close = log(1 + sa_bd_net_npa_closing_bal)
gen log_prov_npa = log(1 + sa_bd_prov_for_npa)

// Bank*Firm groups
egen bank_firm = group(creditorcode sa_finance1_cocode)

// Bank*Time groups
egen bank_quarter = group(creditorcode period_new)

egen firm_quarter = group(sa_finance1_cocode period_new)

keep if period_new >= 28 & period_new <= 30

gen crisis = 1

save "dta_covid/tables.dta", replace

use "raw\LoansInMCA(2).dta", replace

gen year = year(sanctionDate)
gen month = month(sanctionDate)

replace year = year + 1 if month >= 4

bysort creditorCode firmCode year: egen total_sanctioned = sum(loanAmount)

bysort creditorCode firmCode year: gen number_of_loans = _N

gen close_year = year(repaymentDate)
gen close_month = month(repaymentDate)

replace close_year = close_year + 1 if close_month >= 4

drop if creditorCode == . | firmCode == .

duplicates drop creditorCode firmCode year, force

rename (creditorCode firmCode) (creditorcode sa_finance1_cocode)

keep creditorcode sa_finance1_cocode year total_sanctioned number_of_loans close_year

save "dta/sanctioned_year.dta", replace



use "raw\LoansInMCA(2).dta", replace

gen year = year(repaymentDate)
gen month = month(repaymentDate)

replace year = year + 1 if month >= 4

bysort creditorCode firmCode year: egen total_repaid = sum(loanAmount)

drop if creditorCode == . | firmCode == . | year == .

duplicates drop creditorCode firmCode year, force

rename (creditorCode firmCode) (creditorcode sa_finance1_cocode)

keep creditorcode sa_finance1_cocode year total_repaid

save "dta/repayment_year.dta", replace



use "raw\LoansInMCA(2).dta", replace

drop if creditorCode == . | firmCode == .

duplicates drop creditorCode firmCode, force

rename (creditorCode firmCode) (creditorcode sa_finance1_cocode)

keep creditorcode sa_finance1_cocode

save "dta/all_firm_bank_pairs.dta", replace

use "dta/all_firm_bank_pairs.dta", replace

forvalues year = 1/75 {
	append using "dta/all_firm_bank_pairs.dta"
}

bysort creditorcode sa_finance1_cocode: gen year = _n

replace year = year + 1947

save "dta/all_firm_bank_year_pairs.dta", replace

use "dta/all_firm_bank_year_pairs.dta", replace

merge 1:1 creditorcode sa_finance1_cocode year using "dta/sanctioned_year.dta"

drop if _m==2
drop _m

merge 1:1 creditorcode sa_finance1_cocode year using "dta/repayment_year.dta"

drop if _m==2
drop _m

replace total_repaid = 0 if total_repaid == .

gen diff = total_sanctioned - total_repaid

egen firm_bank = group(creditorcode sa_finance1_cocode)

tsset firm_bank year, yearly

gen outstanding = 0
replace outstanding = diff if L1.outstanding == .
replace outstanding = diff + L1.outstanding if L1.outstanding != .

gen loan_completed_5_new = (L1.number_of_loans + L2.number_of_loans + L3.number_of_loans + L4.number_of_loans + L5.number_of_loans)
replace loan_completed_5_new = 0 if loan_completed_5_new == .

gen lag_amount_new = L1.outstanding
gen lag_lg_amount_new = log(1+lag_amount_new)

keep if year >= 2005

keep creditorcode sa_finance1_cocode year loan_completed_5_new lag_amount_new lag_lg_amount_new

save "dta/controls_loans_issue_and_outstanding.dta", replace

use "dta/all_firm_bank_year_pairs.dta", replace

merge 1:1 creditorcode sa_finance1_cocode year using "dta/sanctioned_year.dta"

drop if _m==2
drop _m

replace total_sanctioned = 0 if total_sanctioned == .
replace number_of_loans = 0 if number_of_loans == .

save "dta/interim_avg_loan_tenure.dta", replace



use "dta/interim_avg_loan_tenure.dta", replace

keep if year <= 2005

gen duration_year = 2005 - year

replace close_year = . if close_year > 2005

replace duration_year = (close_year - year) if (close_year < 2005) & close_year != .

gen duration_x_loans = duration_year * number_of_loans

bysort creditorcode sa_finance1_cocode (year): egen sum_duration_x_loans = sum(duration_x_loans)
bysort creditorcode sa_finance1_cocode (year): egen sum_loans = sum(number_of_loans)

gen avg_loan_duration_new = sum_duration_x_loans/sum_loans

keep if year == 2005

save "dta/avg_loan_duration_2005.dta", replace



use "dta/interim_avg_loan_tenure.dta", replace

keep if year <= 2006

gen duration_year = 2006 - year

replace close_year = . if close_year > 2006

replace duration_year = (close_year - year) if (close_year < 2006) & close_year != .

gen duration_x_loans = duration_year * number_of_loans

bysort creditorcode sa_finance1_cocode (year): egen sum_duration_x_loans = sum(duration_x_loans)
bysort creditorcode sa_finance1_cocode (year): egen sum_loans = sum(number_of_loans)

gen avg_loan_duration_new = sum_duration_x_loans/sum_loans

keep if year == 2006

save "dta/avg_loan_duration_2006.dta", replace



use "dta/interim_avg_loan_tenure.dta", replace

keep if year <= 2007

gen duration_year = 2007 - year

replace close_year = . if close_year > 2007

replace duration_year = (close_year - year) if (close_year < 2007) & close_year != .

gen duration_x_loans = duration_year * number_of_loans

bysort creditorcode sa_finance1_cocode (year): egen sum_duration_x_loans = sum(duration_x_loans)
bysort creditorcode sa_finance1_cocode (year): egen sum_loans = sum(number_of_loans)

gen avg_loan_duration_new = sum_duration_x_loans/sum_loans

keep if year == 2007

save "dta/avg_loan_duration_2007.dta", replace



use "dta/interim_avg_loan_tenure.dta", replace

keep if year <= 2008

gen duration_year = 2008 - year

replace close_year = . if close_year > 2008

replace duration_year = (close_year - year) if (close_year < 2008) & close_year != .

gen duration_x_loans = duration_year * number_of_loans

bysort creditorcode sa_finance1_cocode (year): egen sum_duration_x_loans = sum(duration_x_loans)
bysort creditorcode sa_finance1_cocode (year): egen sum_loans = sum(number_of_loans)

gen avg_loan_duration_new = sum_duration_x_loans/sum_loans

keep if year == 2008

save "dta/avg_loan_duration_2008.dta", replace



use "dta/interim_avg_loan_tenure.dta", replace

keep if year <= 2009

gen duration_year = 2009 - year

replace close_year = . if close_year > 2009

replace duration_year = (close_year - year) if (close_year < 2009) & close_year != .

gen duration_x_loans = duration_year * number_of_loans

bysort creditorcode sa_finance1_cocode (year): egen sum_duration_x_loans = sum(duration_x_loans)
bysort creditorcode sa_finance1_cocode (year): egen sum_loans = sum(number_of_loans)

gen avg_loan_duration_new = sum_duration_x_loans/sum_loans

keep if year == 2009

save "dta/avg_loan_duration_2009.dta", replace



use "dta/interim_avg_loan_tenure.dta", replace

keep if year <= 2010

gen duration_year = 2010 - year

replace close_year = . if close_year > 2010

replace duration_year = (close_year - year) if (close_year < 2010) & close_year != .

gen duration_x_loans = duration_year * number_of_loans

bysort creditorcode sa_finance1_cocode (year): egen sum_duration_x_loans = sum(duration_x_loans)
bysort creditorcode sa_finance1_cocode (year): egen sum_loans = sum(number_of_loans)

gen avg_loan_duration_new = sum_duration_x_loans/sum_loans

keep if year == 2010

save "dta/avg_loan_duration_2010.dta", replace



use "dta/interim_avg_loan_tenure.dta", replace

keep if year <= 2011

gen duration_year = 2011 - year

replace close_year = . if close_year > 2011

replace duration_year = (close_year - year) if (close_year < 2011) & close_year != .

gen duration_x_loans = duration_year * number_of_loans

bysort creditorcode sa_finance1_cocode (year): egen sum_duration_x_loans = sum(duration_x_loans)
bysort creditorcode sa_finance1_cocode (year): egen sum_loans = sum(number_of_loans)

gen avg_loan_duration_new = sum_duration_x_loans/sum_loans

keep if year == 2011

save "dta/avg_loan_duration_2011.dta", replace



use "dta/interim_avg_loan_tenure.dta", replace

keep if year <= 2012

gen duration_year = 2012 - year

replace close_year = . if close_year > 2012

replace duration_year = (close_year - year) if (close_year < 2012) & close_year != .

gen duration_x_loans = duration_year * number_of_loans

bysort creditorcode sa_finance1_cocode (year): egen sum_duration_x_loans = sum(duration_x_loans)
bysort creditorcode sa_finance1_cocode (year): egen sum_loans = sum(number_of_loans)

gen avg_loan_duration_new = sum_duration_x_loans/sum_loans

keep if year == 2012

save "dta/avg_loan_duration_2012.dta", replace



use "dta/interim_avg_loan_tenure.dta", replace

keep if year <= 2013

gen duration_year = 2013 - year

replace close_year = . if close_year > 2013

replace duration_year = (close_year - year) if (close_year < 2013) & close_year != .

gen duration_x_loans = duration_year * number_of_loans

bysort creditorcode sa_finance1_cocode (year): egen sum_duration_x_loans = sum(duration_x_loans)
bysort creditorcode sa_finance1_cocode (year): egen sum_loans = sum(number_of_loans)

gen avg_loan_duration_new = sum_duration_x_loans/sum_loans

keep if year == 2013

save "dta/avg_loan_duration_2013.dta", replace



use "dta/interim_avg_loan_tenure.dta", replace

keep if year <= 2019

gen duration_year = 2019 - year

replace close_year = . if close_year > 2019

replace duration_year = (close_year - year) if (close_year < 2019) & close_year != .

gen duration_x_loans = duration_year * number_of_loans

bysort creditorcode sa_finance1_cocode (year): egen sum_duration_x_loans = sum(duration_x_loans)
bysort creditorcode sa_finance1_cocode (year): egen sum_loans = sum(number_of_loans)

gen avg_loan_duration_new = sum_duration_x_loans/sum_loans

keep if year == 2019

save "dta/avg_loan_duration_2019.dta", replace



use "dta/interim_avg_loan_tenure.dta", replace

keep if year <= 2020

gen duration_year = 2020 - year

replace close_year = . if close_year > 2020

replace duration_year = (close_year - year) if (close_year < 2020) & close_year != .

gen duration_x_loans = duration_year * number_of_loans

bysort creditorcode sa_finance1_cocode (year): egen sum_duration_x_loans = sum(duration_x_loans)
bysort creditorcode sa_finance1_cocode (year): egen sum_loans = sum(number_of_loans)

gen avg_loan_duration_new = sum_duration_x_loans/sum_loans

keep if year == 2020

save "dta/avg_loan_duration_2020.dta", replace



use "dta/interim_avg_loan_tenure.dta", replace

keep if year <= 2021

gen duration_year = 2021 - year

replace close_year = . if close_year > 2021

replace duration_year = (close_year - year) if (close_year < 2021) & close_year != .

gen duration_x_loans = duration_year * number_of_loans

bysort creditorcode sa_finance1_cocode (year): egen sum_duration_x_loans = sum(duration_x_loans)
bysort creditorcode sa_finance1_cocode (year): egen sum_loans = sum(number_of_loans)

gen avg_loan_duration_new = sum_duration_x_loans/sum_loans

keep if year == 2021

save "dta/avg_loan_duration_2021.dta", replace



use "dta/avg_loan_duration_2005.dta", replace

forvalues year = 2006/2013{
	append using "dta/avg_loan_duration_`year'.dta"
}

forvalues year = 2019/2021{
	append using "dta/avg_loan_duration_`year'.dta"
}

save "dta/control_avg_loan_duration.dta", replace

********************************************************* Data Creation - Table A6 (Covid - External Validation) (End) *********************************



****************************************************************************************************************************************************
/* Table A6 - Covid (External Validation) */
****************************************************************************************************************************************************

use "dta_covid/tables.dta", replace

merge m:1 creditorcode sa_finance1_cocode year using "dta/controls_loans_issue_and_outstanding.dta"
drop if _m==2
drop _m

merge m:1 creditorcode sa_finance1_cocode year using "dta/control_avg_loan_duration.dta"
drop if _m==2
drop _m

merge m:1 sa_finance1_cocode year using "dta/firm_year_controls.dta"
drop if _m == 2
drop _m

gen lag_amount_assets_new = lag_amount_new/sa_total_assets

reghdfe total_loan_given_dummy    int_treat1_risky1 treat1 risky_firm1 loan_completed_5_new avg_loan_duration_new lag_amount_assets_new  if period_new >= 28 & period_new <= 30, absorb(creditorcode   firm_quarter) cluster(nic_prod_code)
outreg2 using "Referee/tables/table_covid_crisis.xls", replace label ctitle (Loan Dummy) dec(4) se coefastr bracket nocons addtext (Controls, Yes, Bank Fixed Effects, Yes, Bank-Quarter Fixed Effects, No, Firm x Year Fixed Effects, Yes, Bank x Firm Fixed Effects, No)
reghdfe total_loan_given_dummy    int_treat1_risky1 treat1 risky_firm1 loan_completed_5_new avg_loan_duration_new lag_amount_assets_new  if period_new >= 28 & period_new <= 30, absorb(bank_quarter    firm_quarter) cluster(nic_prod_code)
outreg2 using "Referee/tables/table_covid_crisis.xls", append label ctitle (Loan Dummy) dec(4) se coefastr bracket nocons addtext (Controls, Yes, Bank Fixed Effects, No, Bank-Quarter Fixed Effects, Yes, Firm x Year Fixed Effects, Yes, Bank x Firm Fixed Effects, No)
reghdfe total_loan_given_dummy    int_treat1_risky1 treat1 risky_firm1 loan_completed_5_new avg_loan_duration_new lag_amount_assets_new  if period_new >= 28 & period_new <= 30, absorb(bank_quarter  bank_firm  firm_quarter) cluster(nic_prod_code)
outreg2 using "Referee/tables/table_covid_crisis.xls", append label ctitle (Loan Dummy) dec(4) se coefastr bracket nocons addtext (Controls, Yes, Bank Fixed Effects, No, Bank-Quarter Fixed Effects, Yes, Firm x Year Fixed Effects, Yes, Bank x Firm Fixed Effects, Yes)
reghdfe num_of_modifications_dummy     int_treat1_risky1 treat1 risky_firm1 loan_completed_5_new avg_loan_duration_new lag_amount_assets_new  if period_new >= 28 & period_new <= 30, absorb(creditorcode   firm_quarter) cluster(nic_prod_code)
outreg2 using "Referee/tables/table_covid_crisis.xls", append label ctitle (Restructuring) dec(4) se coefastr bracket nocons addtext (Controls, Yes, Bank Fixed Effects, Yes, Bank-Quarter Fixed Effects, No, Firm x Year Fixed Effects, Yes, Bank x Firm Fixed Effects, No)
reghdfe num_of_modifications_dummy     int_treat1_risky1 treat1 risky_firm1 loan_completed_5_new avg_loan_duration_new lag_amount_assets_new  if period_new >= 28 & period_new <= 30, absorb(bank_quarter    firm_quarter) cluster(nic_prod_code)
outreg2 using "Referee/tables/table_covid_crisis.xls", append label ctitle (Restructuring) dec(4) se coefastr bracket nocons addtext (Controls, Yes, Bank Fixed Effects, No, Bank-Quarter Fixed Effects, Yes, Firm x Year Fixed Effects, Yes, Bank x Firm Fixed Effects, No)
reghdfe num_of_modifications_dummy     int_treat1_risky1 treat1 risky_firm1 loan_completed_5_new avg_loan_duration_new lag_amount_assets_new  if period_new >= 28 & period_new <= 30, absorb(bank_quarter    firm_quarter) cluster(nic_prod_code)
outreg2 using "Referee/tables/table_covid_crisis.xls", append label ctitle (Restructuring) dec(4) se coefastr bracket nocons addtext (Controls, Yes, Bank Fixed Effects, No, Bank-Quarter Fixed Effects, Yes, Firm x Year Fixed Effects, Yes, Bank x Firm Fixed Effects, Yes)



****************************************************************************************************************************************************
/* Table A7 - Interest Rate */
****************************************************************************************************************************************************

use "dta/MCA_data_psb_quarter_included.dta", clear

keep if sanctionDate  >= td(01jan2008) & sanctionDate  <= td(30jun2009)

merge m:1 creditorcode using "dta/ceo_change_quarterly_new.dta"
gen treat_any = 0
replace treat_any = 1 if _m == 3 & period>=period_ceo
drop if _m==2 
drop _m

bysort firmcode: egen treat_any_sum = sum(treat_any)
bysort firmcode: gen no_loans = _N

keep if treat_any_sum == 0 | treat_any_sum == no_loans
keep firmcode treat_any_sum no_loans
duplicates drop

save "dta/fully_treated_control_firms.dta", replace

****************************************************************************************************************************************************
/* Firm-year level data having investment information of firms */
****************************************************************************************************************************************************

import delimited "raw\investment.txt", delimiter("|") clear

drop v24 v25

destring sa_total_income-sa_total_loans_and_advances, force replace

tostring sa_finance1_year, force replace
gen day = substr(sa_finance1_year,7,2)
gen month = substr(sa_finance1_year,5,2)
gen year = substr(sa_finance1_year,1,4)

destring day-year, force replace

duplicates tag sa_finance1_cocode year, gen(x)
bysort sa_finance1_cocode: gen march = 1 if x>0 & month == 3
bysort sa_finance1_cocode: egen max_march = max(march) if x>0
drop if x>0 & max_march == 1 & march != 1
sort sa_finance1_cocode year month
bysort sa_finance1_cocode year: gen dup_n = _n if x > 0 & max_march != 1 
drop if dup_n > 1 & dup_n != .
sort sa_finance1_cocode year month day
bysort sa_finance1_cocode year: egen march_dup = sum(march) if x > 0 & max_march == 1 
bysort sa_finance1_cocode year: gen march_dup_n = _n if march_dup > 1 & march_dup != .
bysort sa_finance1_cocode year: gen march_dup_total_n = _N if march_dup > 1 & march_dup != .
drop if march_dup_n < march_dup_total_n & march_dup_n != .

drop x-march_dup_total_n

tsset sa_finance1_cocode year, yearly

gen delta_assets = sa_total_assets - L1.sa_total_assets
gen f1_delta_assets = f1.delta_assets

gen delta_lt_inv = sa_non_curr_investments - L1.sa_non_curr_investments
gen f1_delta_lt_inv = f1.delta_lt_inv

gen delta_st_inv = sa_short_term_investments - L1.sa_short_term_investments
gen f1_delta_st_inv = f1.delta_st_inv

gen delta_ppe = sa_net_ppe - L1.sa_net_ppe
gen f1_delta_ppe = f1.delta_ppe

gen delta_grossppe = sa_gross_property_plant_and_equi - L1.sa_gross_property_plant_and_equi
gen f1_delta_grossppe = f1.delta_grossppe

gen delta_pmc = sa_net_plant_mach_computer_elec - L1.sa_net_plant_mach_computer_elec
gen f1_delta_pmc = f1.delta_pmc

gen delta_plant = sa_net_plant - L1.sa_net_plant
gen f1_delta_plant = f1.delta_plant

gen f1_assets = f1.sa_total_assets
gen f1_ppe = f1.sa_net_ppe

gen per_chg_assets = 100*(sa_total_assets - L1.sa_total_assets)/L1.sa_total_assets
gen f1_per_chg_assets = f1.per_chg_assets

gen per_chg_ppe = 100*(sa_net_ppe - L1.sa_net_ppe)/L1.sa_net_ppe
gen f1_per_chg_ppe = f1.per_chg_ppe

gen per_chg_ppe2 = 100*(sa_net_ppe - L2.sa_net_ppe)/L2.sa_net_ppe
gen f1_per_chg_ppe2 = f1.per_chg_ppe2

gen per_chg_ppe3 = 100*(sa_net_ppe - L3.sa_net_ppe)/L3.sa_net_ppe
gen f1_per_chg_ppe3 = f1.per_chg_ppe3

gen per_chg_ppe4 = 100*(sa_net_ppe - L4.sa_net_ppe)/L4.sa_net_ppe
gen f1_per_chg_ppe4 = f1.per_chg_ppe4

gen per_chg_ppe5 = 100*(sa_net_ppe - L5.sa_net_ppe)/L5.sa_net_ppe
gen f1_per_chg_ppe5 = f1.per_chg_ppe5

gen per_chg_grossppe = 100*(sa_gross_property_plant_and_equi - L1.sa_gross_property_plant_and_equi)/L1.sa_gross_property_plant_and_equi
gen f1_per_chg_grossppe = f1.per_chg_grossppe

gen per_chg_pmc = 100*(sa_net_plant_mach_computer_elec - L1.sa_net_plant_mach_computer_elec)/L1.sa_net_plant_mach_computer_elec
gen f1_per_chg_pmc = f1.per_chg_pmc

gen per_chg_plant = 100*(sa_net_plant - L1.sa_net_plant)/L1.sa_net_plant
gen f1_per_chg_plant = f1.per_chg_plant

gen per_chg_ppe_f1 = 100*(f1.sa_net_ppe - sa_net_ppe)/sa_net_ppe
gen per_chg_ppe_f2 = 100*(f2.sa_net_ppe - sa_net_ppe)/sa_net_ppe
gen per_chg_ppe_f3 = 100*(f3.sa_net_ppe - sa_net_ppe)/sa_net_ppe
gen per_chg_ppe_f4 = 100*(f4.sa_net_ppe - sa_net_ppe)/sa_net_ppe
gen per_chg_ppe_f5 = 100*(f5.sa_net_ppe - sa_net_ppe)/sa_net_ppe

gen per_chg_grossppe_f1 = 100*(f1.sa_gross_property_plant_and_equi - sa_gross_property_plant_and_equi)/sa_gross_property_plant_and_equi
gen per_chg_grossppe_f2 = 100*(f2.sa_gross_property_plant_and_equi - sa_gross_property_plant_and_equi)/sa_gross_property_plant_and_equi
gen per_chg_grossppe_f3 = 100*(f3.sa_gross_property_plant_and_equi - sa_gross_property_plant_and_equi)/sa_gross_property_plant_and_equi
gen per_chg_grossppe_f4 = 100*(f4.sa_gross_property_plant_and_equi - sa_gross_property_plant_and_equi)/sa_gross_property_plant_and_equi
gen per_chg_grossppe_f5 = 100*(f5.sa_gross_property_plant_and_equi - sa_gross_property_plant_and_equi)/sa_gross_property_plant_and_equi

gen per_chg_pmc_f1 = 100*(f1.sa_net_plant_mach_computer_elec - sa_net_plant_mach_computer_elec)/sa_net_plant_mach_computer_elec
gen per_chg_pmc_f2 = 100*(f2.sa_net_plant_mach_computer_elec - sa_net_plant_mach_computer_elec)/sa_net_plant_mach_computer_elec
gen per_chg_pmc_f3 = 100*(f3.sa_net_plant_mach_computer_elec - sa_net_plant_mach_computer_elec)/sa_net_plant_mach_computer_elec
gen per_chg_pmc_f4 = 100*(f4.sa_net_plant_mach_computer_elec - sa_net_plant_mach_computer_elec)/sa_net_plant_mach_computer_elec
gen per_chg_pmc_f5 = 100*(f5.sa_net_plant_mach_computer_elec - sa_net_plant_mach_computer_elec)/sa_net_plant_mach_computer_elec

gen per_chg_assets_f1 = 100*(f1.sa_total_assets - sa_total_assets)/sa_total_assets
gen per_chg_assets_f2 = 100*(f2.sa_total_assets - sa_total_assets)/sa_total_assets
gen per_chg_assets_f3 = 100*(f3.sa_total_assets - sa_total_assets)/sa_total_assets
gen per_chg_assets_f4 = 100*(f4.sa_total_assets - sa_total_assets)/sa_total_assets
gen per_chg_assets_f5 = 100*(f5.sa_total_assets - sa_total_assets)/sa_total_assets

gen per_chg_plant_f1 = 100*(f1.sa_net_plant - sa_net_plant)/sa_net_plant
gen per_chg_plant_f2 = 100*(f2.sa_net_plant - sa_net_plant)/sa_net_plant
gen per_chg_plant_f3 = 100*(f3.sa_net_plant - sa_net_plant)/sa_net_plant
gen per_chg_plant_f4 = 100*(f4.sa_net_plant - sa_net_plant)/sa_net_plant
gen per_chg_plant_f5 = 100*(f5.sa_net_plant - sa_net_plant)/sa_net_plant

gen per_chg_income_f1 = 100*(f1.sa_total_income - sa_total_income)/sa_total_income
gen per_chg_income_f2 = 100*(f2.sa_total_income - sa_total_income)/sa_total_income
gen per_chg_income_f3 = 100*(f3.sa_total_income - sa_total_income)/sa_total_income
gen per_chg_income_f4 = 100*(f4.sa_total_income - sa_total_income)/sa_total_income
gen per_chg_income_f5 = 100*(f5.sa_total_income - sa_total_income)/sa_total_income

gen per_chg_sales_f1 = 100*(f1.sa_sales - sa_sales)/sa_sales
gen per_chg_sales_f2 = 100*(f2.sa_sales - sa_sales)/sa_sales
gen per_chg_sales_f3 = 100*(f3.sa_sales - sa_sales)/sa_sales
gen per_chg_sales_f4 = 100*(f4.sa_sales - sa_sales)/sa_sales
gen per_chg_sales_f5 = 100*(f5.sa_sales - sa_sales)/sa_sales

gen per_chg_pat_f1 = 100*(f1.sa_pat - sa_pat)/sa_pat
gen per_chg_pat_f2 = 100*(f2.sa_pat - sa_pat)/sa_pat
gen per_chg_pat_f3 = 100*(f3.sa_pat - sa_pat)/sa_pat
gen per_chg_pat_f4 = 100*(f4.sa_pat - sa_pat)/sa_pat
gen per_chg_pat_f5 = 100*(f5.sa_pat - sa_pat)/sa_pat

gen log_income_growth1 = log(f1.sa_total_income/sa_total_income)
gen log_income_growth2 = log(f2.sa_total_income/sa_total_income)
gen log_income_growth3 = log(f3.sa_total_income/sa_total_income)
gen log_income_growth4 = log(f4.sa_total_income/sa_total_income)
gen log_income_growth5 = log(f5.sa_total_income/sa_total_income)

gen log_sales_growth1 = log(f1.sa_sales/sa_sales)
gen log_sales_growth2 = log(f2.sa_sales/sa_sales)
gen log_sales_growth3 = log(f3.sa_sales/sa_sales)
gen log_sales_growth4 = log(f4.sa_sales/sa_sales)
gen log_sales_growth5 = log(f5.sa_sales/sa_sales)

gen log_pat_growth1 = log(f1.sa_pat/sa_pat)
gen log_pat_growth2 = log(f2.sa_pat/sa_pat)
gen log_pat_growth3 = log(f3.sa_pat/sa_pat)
gen log_pat_growth4 = log(f4.sa_pat/sa_pat)
gen log_pat_growth5 = log(f5.sa_pat/sa_pat)

gen log_ppe_growth1 = log(f1.sa_net_ppe/sa_net_ppe)
gen log_ppe_growth2 = log(f2.sa_net_ppe/sa_net_ppe)
gen log_ppe_growth3 = log(f3.sa_net_ppe/sa_net_ppe)
gen log_ppe_growth4 = log(f4.sa_net_ppe/sa_net_ppe)
gen log_ppe_growth5 = log(f5.sa_net_ppe/sa_net_ppe)

gen log_grossppe_growth1 = log(f1.sa_gross_property_plant_and_equi/sa_gross_property_plant_and_equi)
gen log_grossppe_growth2 = log(f2.sa_gross_property_plant_and_equi/sa_gross_property_plant_and_equi)
gen log_grossppe_growth3 = log(f3.sa_gross_property_plant_and_equi/sa_gross_property_plant_and_equi)
gen log_grossppe_growth4 = log(f4.sa_gross_property_plant_and_equi/sa_gross_property_plant_and_equi)
gen log_grossppe_growth5 = log(f5.sa_gross_property_plant_and_equi/sa_gross_property_plant_and_equi)

gen log_pmc_growth1 = log(f1.sa_net_plant_mach_computer_elec/sa_net_plant_mach_computer_elec)
gen log_pmc_growth2 = log(f2.sa_net_plant_mach_computer_elec/sa_net_plant_mach_computer_elec)
gen log_pmc_growth3 = log(f3.sa_net_plant_mach_computer_elec/sa_net_plant_mach_computer_elec)
gen log_pmc_growth4 = log(f4.sa_net_plant_mach_computer_elec/sa_net_plant_mach_computer_elec)
gen log_pmc_growth5 = log(f5.sa_net_plant_mach_computer_elec/sa_net_plant_mach_computer_elec)

gen log_assets_growth1 = log(f1.sa_total_assets/sa_total_assets)
gen log_assets_growth2 = log(f2.sa_total_assets/sa_total_assets)
gen log_assets_growth3 = log(f3.sa_total_assets/sa_total_assets)
gen log_assets_growth4 = log(f4.sa_total_assets/sa_total_assets)
gen log_assets_growth5 = log(f5.sa_total_assets/sa_total_assets)

gen log_plant_growth1 = log(f1.sa_net_plant/sa_net_plant)
gen log_plant_growth2 = log(f2.sa_net_plant/sa_net_plant)
gen log_plant_growth3 = log(f3.sa_net_plant/sa_net_plant)
gen log_plant_growth4 = log(f4.sa_net_plant/sa_net_plant)
gen log_plant_growth5 = log(f5.sa_net_plant/sa_net_plant)

save "dta/investment.dta", replace


use "dta/bank_interest_data.dta", clear

merge m:1 firmcode using "dta/fully_treated_control_firms.dta"
keep if _m == 3
drop _m

gen sa_finance1_cocode = firmcode
merge m:1 sa_finance1_cocode year using "dta/investment.dta", force
drop if _m==2
drop _m

merge m:1 sa_finance1_cocode using "dta/industry.dta"
drop if _m==2
drop _m

merge m:1 sa_finance1_cocode using "dta/firms_interest_coverage.dta"
drop if _m==2
drop _m

gen bank_borrowings = sa_bank_borrowings/total_outstanding
keep if bank_borrowings>0.75

gen log_income = log(1 + sa_total_income)
gen log_pat = log(1 + sa_pat)
gen pat_income = sa_pat/sa_total_income

gen treated_firm = 1 if treat_any_sum == no_loans
replace treated_firm = 0 if treat_any_sum == 0

gen interaction = treated_firm*risky_firm1

gen effective_int_1_w_perc = 100*effective_int_1_w

eststo clear
label var interaction "Treated firm X Risky firm"
label var treated_firm "Treated firm"
label var risky_firm1 "Risky firm"

reghdfe effective_int_1_w_perc interaction treated_firm risky_firm1 if year>=2008 & year<=2010, absorb(year) cluster(nic_prod_code)
outreg2 using "Referee/tables/table_interest_rate.xls", replace label ctitle (Loan Dummy) dec(4) se coefastr bracket nocons addtext (Controls, No, Year Fixed Effects, Yes)

reghdfe effective_int_1_w_perc interaction treated_firm risky_firm1 log_income pat_income if year>=2008 & year<=2010, absorb(year) cluster(nic_prod_code) 
outreg2 using "Referee/tables/table_interest_rate.xls", append label ctitle (Loan Dummy) dec(4) se coefastr bracket nocons addtext (Controls, Yes, Year Fixed Effects, Yes)



****************************************************************************************************************************************************
/* Table A8 - Banks with data on CEO age */
****************************************************************************************************************************************************

use "dta/tables.dta", replace

drop int_treat1_risky1

replace treat1 = 0 if creditorcode == 236337

gen int_treat1_risky1 = treat1*risky_firm1

// Bank*Firm groups
egen bank_firm = group(creditorcode sa_finance1_cocode)

// Bank*Time groups
egen bank_year = group(creditorcode year)

gen firmCode = sa_finance1_cocode
gen creditorCode = creditorcode
merge m:1 firmCode creditorCode year using "dta/FirmBankYearControls_2018 - Copy.dta"
drop if _m == 2
drop _m

merge m:1 sa_finance1_cocode year using "dta/firm_year_controls.dta"
drop if _m == 2
drop _m

gen lag_amount = exp(lag_lg_amount)
gen lag_amount_assets = lag_amount/(sa_total_assets*1000000)

merge m:1 creditorcode sa_finance1_cocode year quarter using "dta/restructuring_psb_final_extended_industry_crisis.dta"

replace num_of_modifications = 0 if _m==1
replace num_of_modifications2 = 0 if _m==1

drop if _m==2
drop _m

gen num_of_modifications_dummy = 0
replace num_of_modifications_dummy = 1 if num_of_modifications > 0

gen num_of_modifications_dummy2 = 0
replace num_of_modifications_dummy2 = 1 if num_of_modifications2 > 0

// Bank*Time groups
egen bank_quarter = group(creditorcode period_new)


gen no_data = .
replace no_data = 0 if creditorcode != .
replace no_data = 1 if creditorcode == 11019 & period_new >= 30
replace no_data = 1 if creditorcode == 30153 & period_new >= 29
replace no_data = 1 if creditorcode == 257827 & period_new >= 31


gen treat_risky_nodata = treat1*risky_firm1*no_data
gen treat_risky = treat1*risky_firm1
gen risky_nodata = risky_firm1*no_data
gen treat_nodata = treat1*no_data


eststo clear
label variable treat_risky_nodata "CEO change x Low-quality firm x No Data"
label variable treat_risky "CEO change x Low-quality firm"
label variable risky_nodata "Low-quality firm x No Data"
label variable treat_nodata "CEO change x No Data"
label variable treat1 "CEO change"



reghdfe total_loan_given_dummy treat_risky treat1 risky_firm1 no_data avg_loan_duration loan_completed_5 lag_amount_assets if period_new >= 28 & period_new <= 33 & no_data == 0, absorb(creditorcode firm_year) cluster(nic_prod_code)
outreg2 using "Referee/tables/table_no_data_large_firm.xls", replace label ctitle (Loan Dummy) dec(4) se coefastr bracket nocons addtext (Controls, Yes, Bank Fixed Effects, Yes, Bank-Quarter Fixed Effects, No, Firm x Year Fixed Effects, Yes, Bank-Firm Fixed Effects, No)

reghdfe total_loan_given_dummy treat_risky treat1 risky_firm1 no_data avg_loan_duration loan_completed_5 lag_amount_assets if period_new >= 28 & period_new <= 33 & no_data == 0, absorb(bank_quarter  firm_year) cluster(nic_prod_code)
outreg2 using "Referee/tables/table_no_data_large_firm.xls", append label ctitle (Loan Dummy) dec(4) se coefastr bracket nocons addtext (Controls, Yes, Bank Fixed Effects, No, Bank-Quarter Fixed Effects, Yes, Firm x Year Fixed Effects, Yes, Bank-Firm Fixed Effects, No)

reghdfe total_loan_given_dummy treat_risky treat1 risky_firm1 no_data avg_loan_duration loan_completed_5 lag_amount_assets if period_new >= 28 & period_new <= 33 & no_data == 0, absorb(bank_quarter  firm_year bank_firm ) cluster(nic_prod_code)
outreg2 using "Referee/tables/table_no_data_large_firm.xls", append label ctitle (Loan Dummy) dec(4) se coefastr bracket nocons addtext (Controls, Yes, Bank Fixed Effects, No, Bank-Quarter Fixed Effects, Yes, Firm x Year Fixed Effects, Yes, Bank-Firm Fixed Effects, Yes)

reghdfe num_of_modifications_dummy treat_risky treat1 risky_firm1 no_data avg_loan_duration loan_completed_5 lag_amount_assets if period_new >= 28 & period_new <= 33 & no_data == 0, absorb(creditorcode firm_year) cluster(nic_prod_code)
outreg2 using "Referee/tables/table_no_data_large_firm.xls", append label ctitle (Restructuring) dec(4) se coefastr bracket nocons addtext (Controls, Yes, Bank Fixed Effects, Yes, Bank-Quarter Fixed Effects, No, Firm x Year Fixed Effects, Yes, Bank-Firm Fixed Effects, No)

reghdfe num_of_modifications_dummy treat_risky treat1 risky_firm1 no_data avg_loan_duration loan_completed_5 lag_amount_assets if period_new >= 28 & period_new <= 33 & no_data == 0, absorb(bank_quarter  firm_year) cluster(nic_prod_code)
outreg2 using "Referee/tables/table_no_data_large_firm.xls", append label ctitle (Restructuring) dec(4) se coefastr bracket nocons addtext (Controls, Yes, Bank Fixed Effects, No, Bank-Quarter Fixed Effects, Yes, Firm x Year Fixed Effects, Yes, Bank-Firm Fixed Effects, No)

reghdfe num_of_modifications_dummy  treat_risky treat1 risky_firm1 no_data avg_loan_duration loan_completed_5 lag_amount_assets if period_new >= 28 & period_new <= 33 & no_data == 0, absorb(bank_quarter  firm_year bank_firm ) cluster(nic_prod_code)
outreg2 using "Referee/tables/table_no_data_large_firm.xls", append label ctitle (Restructuring) dec(4) se coefastr bracket nocons addtext (Controls, Yes, Bank Fixed Effects, No, Bank-Quarter Fixed Effects, Yes, Firm x Year Fixed Effects, Yes, Bank-Firm Fixed Effects, Yes)



****************************************************************************************************************************************************
/* Table A10 - Accouting vs Marketing Performance */
****************************************************************************************************************************************************

use "dta/chairman_firm_year_matching.dta", replace
drop in 11/12
drop in 120/122
save "dta/chairman_firm_year_matching_new.dta", replace

use "dta/bank_info_next_position.dta", replace

merge m:1 year using "dta/nifty50_data.dta"
drop if _m==2
drop _m

merge m:1 year using "dta/niftybank_data.dta"
drop if _m==2
drop _m

merge m:1 creditorcode year using "dta/stock_data_banks.dta"
drop if _m==2
drop _m

merge m:1 creditorcode year using "dta/chairman_firm_year_matching_new.dta"

bysort creditorcode chairman: egen max_year = max(year) if _m==3
bysort creditorcode chairman: egen min_year = min(year) if _m==3

bysort creditorcode: egen max_max_year = max(max_year)
bysort creditorcode: egen min_min_year = min(min_year)

keep if (year >= (min_min_year - 1)) & (year <= max_max_year)

bysort creditorcode chairman: gen count = _N

tsset creditorcode year

keep if (year == max_year) | (year == (min_min_year - 1))

sort creditorcode year

gen pat_growth = (((sa_pat/sa_pat[_n-1])^(1/count)) - 1)
gen pbt_growth = (((sa_pbt/sa_pbt[_n-1])^(1/count)) - 1)
gen pbit_growth = (((sa_pbit/sa_pbit[_n-1])^(1/count)) - 1)
gen pbpt_growth = (((sa_pbpt/sa_pbpt[_n-1])^(1/count)) - 1)
gen pbdita_growth = (((sa_pbdita/sa_pbdita[_n-1])^(1/count)) - 1)
gen loan_growth = (((sa_total_loans_and_advances/sa_total_loans_and_advances[_n-1])^(1/count)) - 1)
gen asset_growth = (((sa_total_assets/sa_total_assets[_n-1])^(1/count)) - 1)
gen income_growth = (((sa_total_income/sa_total_income[_n-1])^(1/count)) - 1)
gen nnpa_close_growth = (((sa_bd_net_npa_closing_bal/sa_bd_net_npa_closing_bal[_n-1])^(1/count)) - 1)
gen nnpa_open_growth = (((sa_bd_net_npa_opening_bal/sa_bd_net_npa_opening_bal[_n-1])^(1/count)) - 1)
gen gnpa_close_growth = (((sa_bd_gross_npa_closing_bal/sa_bd_gross_npa_closing_bal[_n-1])^(1/count)) - 1)
gen gnpa_open_growth = (((sa_bd_gross_npa_opening_bal/sa_bd_gross_npa_opening_bal[_n-1])^(1/count)) - 1)
gen quick_ratio_change = (sa_quick_ratio - sa_quick_ratio[_n-1])/count
gen current_ratio_change = (sa_current_ratio - sa_current_ratio[_n-1])/count
gen debt_equity_change = (sa_debt_equity - sa_debt_equity[_n-1])/count
gen nnpa_close_ratio = sa_bd_net_npa_closing_bal/sa_total_loans_and_advances
gen nnpa_open_ratio = sa_bd_net_npa_opening_bal/sa_total_loans_and_advances
gen gnpa_close_ratio = sa_bd_gross_npa_closing_bal/sa_total_loans_and_advances
gen gnpa_open_ratio = sa_bd_gross_npa_opening_bal/sa_total_loans_and_advances
gen nnpa_close_ratio_change = (nnpa_close_ratio - nnpa_close_ratio[_n-1])/count
gen nnpa_open_ratio_change = (nnpa_open_ratio - nnpa_open_ratio[_n-1])/count
gen gnpa_close_ratio_change = (gnpa_close_ratio - gnpa_close_ratio[_n-1])/count
gen gnpa_open_ratio_change = (gnpa_open_ratio - gnpa_open_ratio[_n-1])/count

gen stock_growth = (((bse_closing_price/bse_closing_price[_n-1])^(1/count)) - 1)
gen nifty50_growth = (((nifty50_price/nifty50_price[_n-1])^(1/count)) - 1)
gen niftybank_growth = (((niftybank_price/niftybank_price[_n-1])^(1/count)) - 1)

gen excess_growth = stock_growth - nifty50_growth
gen excessbank_growth = stock_growth - niftybank_growth

keep if _m==3
drop _m

summ pat_growth pbt_growth pbit_growth pbpt_growth loan_growth asset_growth income_growth nnpa_close_growth nnpa_open_growth gnpa_close_growth gnpa_open_growth quick_ratio_change current_ratio_change debt_equity_change, d

xtile stock_growth_med = stock_growth, nq(2)
replace stock_growth_med = stock_growth_med - 1

xtile excess_growth_med = excess_growth, nq(2)
replace excess_growth_med = excess_growth_med - 1

xtile excessbank_growth_med = excessbank_growth, nq(2)
replace excessbank_growth_med = excessbank_growth_med - 1

xtile pat_growth_med = pat_growth, nq(2)
replace pat_growth_med = pat_growth_med - 1

xtile pbt_growth_med = pbt_growth, nq(2)
replace pbt_growth_med = pbt_growth_med - 1

xtile pbit_growth_med = pbit_growth, nq(2)
replace pbit_growth_med = pbit_growth_med - 1

xtile pbpt_growth_med = pbpt_growth, nq(2)
replace pbpt_growth_med = pbpt_growth_med - 1

xtile pbdita_growth_med = pbdita_growth, nq(2)
replace pbdita_growth_med = pbdita_growth_med - 1

xtile loan_growth_med = loan_growth, nq(2)
replace loan_growth_med = loan_growth_med - 1

xtile asset_growth_med = asset_growth, nq(2)
replace asset_growth_med = asset_growth_med - 1

xtile income_growth_med = income_growth, nq(2)
replace income_growth_med = income_growth_med - 1

xtile nnpa_close_growth_med = nnpa_close_growth, nq(2)
replace nnpa_close_growth_med = nnpa_close_growth_med - 1

xtile nnpa_open_growth_med = nnpa_open_growth, nq(2)
replace nnpa_open_growth_med = nnpa_open_growth_med - 1

xtile gnpa_close_growth_med = gnpa_close_growth, nq(2)
replace gnpa_close_growth_med = gnpa_close_growth_med - 1

xtile gnpa_open_growth_med = gnpa_open_growth, nq(2)
replace gnpa_open_growth_med = gnpa_open_growth_med - 1

xtile quick_ratio_change_med = quick_ratio_change, nq(2)
replace quick_ratio_change_med = quick_ratio_change_med - 1

xtile current_ratio_change_med = current_ratio_change, nq(2)
replace current_ratio_change_med = current_ratio_change_med - 1

xtile debt_equity_change_med = debt_equity_change, nq(2)
replace debt_equity_change_med = debt_equity_change_med - 1

xtile nnpa_close_ratio_change_med = nnpa_close_ratio_change, nq(2)
replace nnpa_close_ratio_change_med = nnpa_close_ratio_change_med - 1

xtile nnpa_open_ratio_change_med = nnpa_open_ratio_change, nq(2)
replace nnpa_open_ratio_change_med = nnpa_open_ratio_change_med - 1

xtile gnpa_close_ratio_change_med = gnpa_close_ratio_change, nq(2)
replace gnpa_close_ratio_change_med = gnpa_close_ratio_change_med - 1

xtile gnpa_open_ratio_change_med = gnpa_open_ratio_change, nq(2)
replace gnpa_open_ratio_change_med = gnpa_open_ratio_change_med - 1

gen pat_growth_d = 0
replace pat_growth_d = 1 if pat_growth > 0

gen pbt_growth_d = 0
replace pbt_growth_d = 1 if pbt_growth > 0

gen pbit_growth_d = 0
replace pbit_growth_d = 1 if pbit_growth > 0

gen pbpt_growth_d = 0
replace pbpt_growth_d = 1 if pbpt_growth > 0

gen loan_growth_d = 0
replace loan_growth_d = 1 if loan_growth > 0

gen asset_growth_d = 0
replace asset_growth_d = 1 if asset_growth > 0

gen income_growth_d = 0
replace income_growth_d = 1 if income_growth > 0

gen nnpa_close_growth_d = 0
replace nnpa_close_growth_d = 1 if nnpa_close_growth > 0

gen nnpa_open_growth_d = 0
replace nnpa_open_growth_d = 1 if nnpa_open_growth > 0

gen gnpa_close_growth_d = 0
replace gnpa_close_growth_d = 1 if gnpa_close_growth > 0

gen gnpa_open_growth_d = 0
replace gnpa_open_growth_d = 1 if gnpa_open_growth > 0

summ pbt_growth_med, d
summ pbt_growth_med if during_covid != 1, d
summ sig_pos  if during_covid != 1, d

merge m:1 creditorcode using "dta/banks_traded_frequently.dta"
keep if _m==3
drop _m

summ excess_growth_med, d

ttest pbt_growth_med, by(sig_pos)
ttest excess_growth_med, by(sig_pos)



****************************************************************************************************************************************************
/* Table A11 -  Eventual Performance of Low Quality Loans */
****************************************************************************************************************************************************

use "dta/new_defaults_all_quarter_srinivas_upto202303_v1.dta", replace

rename firmcode sa_finance1_cocode

gen period_new = 4*(year-2002) + quarter

keep if period_new >= 28 & period_new <= 45

rename new_default new_default_last_ceo

duplicates drop creditorcode sa_finance1_cocode, force

keep creditorcode sa_finance1_cocode new_default_last_ceo

save "dta/new_defaults_cibil_cleaned_last_ceo.dta", replace

use "dta/wilful_defaults_quarterly.dta", replace

keep if period_wilful >= 28 & period_wilful <= 45

duplicates drop creditorcode sa_finance1_cocode, force

keep creditorcode sa_finance1_cocode wilful_defaults

save "dta/wilful_defaults_quarterly_last_ceo.dta", replace

use "dta/tables.dta", replace

keep if period_new == 33

// Bank*Firm groups
egen bank_firm = group(creditorcode sa_finance1_cocode)

// Bank*Time groups
egen bank_quarter = group(creditorcode period_new)

gen firmCode = sa_finance1_cocode
gen creditorCode = creditorcode
merge m:1 firmCode creditorCode year using "dta/FirmBankYearControls_2018 - Copy.dta"
drop if _m == 2
drop _m

merge m:1 sa_finance1_cocode year using "dta/firm_year_controls.dta"
drop if _m == 2
drop _m

gen lag_amount = exp(lag_lg_amount)
gen lag_amount_assets = lag_amount/(sa_total_assets*1000000)

merge m:1 creditorcode sa_finance1_cocode using "dta/new_defaults_cibil_cleaned_last_ceo.dta"

replace new_default_last_ceo = 0 if _m==1
drop if _m==2
drop _m

merge m:1 creditorcode sa_finance1_cocode using "dta/wilful_defaults_quarterly_last_ceo.dta"

replace wilful_defaults = 0 if _m==1
drop if _m==2
drop _m

gen def_dummy3 = 0
replace def_dummy3 = 1 if new_default_last_ceo  == 1 | wilful_defaults == 1

//45
reghdfe def_dummy3   int_treat1_risky1 treat1 risky_firm1 avg_loan_duration loan_completed_5 lag_amount_assets, absorb(creditorcode  sa_finance1_cocode) cluster(nic_prod_code)
outreg2 using "Referee/tables/point_1_default.xls", append label ctitle (Loan Dummy) dec(4) se coefastr bracket nocons addtext (Controls, Yes, Bank x Quarter Fixed Effects, Yes, Firm x Year Fixed Effects, Yes)

reghdfe new_default_last_ceo     int_treat1_risky1 treat1 risky_firm1 avg_loan_duration loan_completed_5 lag_amount_assets, absorb(creditorcode  sa_finance1_cocode) cluster(nic_prod_code)
outreg2 using "Referee/tables/point_1_default.xls", append label ctitle (Loan Dummy) dec(4) se coefastr bracket nocons addtext (Controls, Yes, Bank x Quarter Fixed Effects, Yes, Firm x Year Fixed Effects, Yes)
